Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<pre><ComboBox x:Name="cbCostType" ItemsSource="{Binding}" SelectedValuePath="CostTypId" DisplayMemberPath="CostTypeName" Canvas.Left="176" Canvas.Top="118" Width="214" TabIndex="5" SelectionChanged="cbCostType_SelectionChanged"/>


C#
<pre>public class CostType
    {
       public int CostTypeId { set; get; }
       public string CostTypeName { set; get; }

       public CostType(int costTypeId, string costTypeName)
       {
           this.CostTypeId = costTypeId;
           this.CostTypeName = costTypeName;
       }
    }



C#
<pre> public static List<CostType> FillDriverCostTypeCombo()
        {
            var costList = new List<CostType>();
            var costIncluded = new CostType(0, "Included");
            costList.Add(costIncluded);
            var costMonth = new CostType(1, "Monthly");
            costList.Add(costMonth);
            var costDay = new CostType(2, "Daily");
            costList.Add(costDay);
            return costList;
        }


What I have tried:

I've tried the normal way
C#
cbCostType.selectedValue=1;

Second way i've tried
C#
var costTypeitem=new CostType(1,"monthly");
cbCostType.selectedItem=costTypeitem;


the only way works is using SelectedIndex
Posted
Updated 15-Jan-21 13:13pm

1 solution

Yes, it's .SelectedIndex, not "selectedValue".

A .SelectedItem refers to an "existing" object in the ItemSource. You can't just make up another object and expect a match (unless it's a value type; like a string).
 
Share this answer
 
Comments
Rabee3-F1.787545 16-Jan-21 14:20pm    
if i want to make selected item CostTypeId = 1 how can u give me an example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900