tags:

views:

135

answers:

1

Hi,

I am not able to set the selected value of a combobox.

this is how i am doing.

ComboBox x:Name="cmbProjectStatus" ItemsSource="{Binding ItemListCollection}" 
         DisplayMemberPath="Name" 
         SelectedValuePath="ID" 
         SelectedValue="{Binding Path=ItemList.ID}"
         SelectedItem="{Binding Path=ItemList}" 
         HorizontalAlignment="Stretch" VerticalAlignment="Center" />

I am using MVVM pattern in my project

Please Help...

+1  A: 

but wait, your selected value is defined because you set selecteditem and selectedvaluepath ;)you don't have to set selectedvalue, and
EDIT
ItemList seted as SelectedItem exists in ItemListCollection

This should work

   ComboBox x:Name="cmbProjectStatus" ItemsSource="{Binding ItemListCollection}" 
             DisplayMemberPath="Name" 
             SelectedValuePath="ID"
             SelectedItem="{Binding Path=ItemList}" 
             HorizontalAlignment="Stretch" VerticalAlignment="Center" />

if you want to get it worked in your case just override Equals method in your Item class like this

public class Item
    {
        ...
        public override bool Equals(object obj)
        {
            Item i = (Item)obj;
            if (i.ID == this.ID)
                return true;
            return base.Equals(obj);
        }
        ...
    }
ArsenMkrt
I tried without setting the SelectedValue, Still the value is not getting selected on form load.
I have a combobox in usercontrol which is loaded while navigating to the treeview. So whenever the Window is loaded the combobox correctly shows the selected value, but while navigating through the treeview it does not show the selected item.
Is ItemList seted as SelectedItem exists in ItemListCollection? ItemList should be element from ItemListCollection
ArsenMkrt
The DataContext has a ItemList property of type Item and a ItemListCollection of type ObservableCollection<Item>.
the types are not so important, shortly said combobox searches reference equality when setting SelectedItem, and if he can't find he doesn't select anything, try to override Equals method of Item type, I think combobox call Equals to know are that objects equal, and because you are not implemented, it uses reference equality
ArsenMkrt
didn't get you. Can you please paste the code here?
I edited the post
ArsenMkrt
Thanks you so much..One question to set the value so that i save it in database..We use Mode=Twoway to set the value. In this case how to do the same?
You are wellcome:) You can save cmbProjectStatus.SelectedValue to database, or I didn't get the questin, try to write new question in details, or to edit your questin
ArsenMkrt
SelectedItem="{Binding Path=ItemList,Mode=TwoWay}" If we set Mode =TwoWay then it will set the value of ItemList Property on selecting any item from the combobox.
I think it will set the value of ItemList Property and without Mode=TwoWay, try that should work
ArsenMkrt
But where we specify to set the ItemList property. Because the we are already doing SelectedItem="{Binding Path=[3]}" . Can you please send the finaly xaml code with itemlist set ?
try {Binding Path=ItemList} with overrided Equals method, It should work, I edited the post
ArsenMkrt
Thanks you very much...
You are wellcome, just mark post as answer if it helps :)
ArsenMkrt