Xaml:
<ComboBox Grid.Row="1"
Name="CmbClasse"
Grid.Column="3"
Margin="5,5,5,5"
ItemsSource="{Binding DataContext.Classeses}"
SelectedValuePath="ClasseId"
DisplayMemberPath="ClasseName"
SelectedValue="{Binding DataContext.CurrentSelectedPersonagem.IdClasse, Mode=TwoWay}"/>
When I open my application, the ComboBox do not have display any default item. But the item list is working, have all the items on it. Please help, if a image is needed just ask. Thanks.
Here is a method I use all the time.
Firstly, your View Model should have two properties, one to represent the collection, and the other to represent the selected item.
Note: It would be a good idea to use an
ObservableCollection
forMyObjects
, and also implementINotifyPropertyChanged
. Use this only if you need to create brand new instances of your collection in the View Model.Your
ComboBox
should then bind to these properties, like so:Now, here comes the magic. When you create your collection, simply set the
SelectedObject
to be theFirstOrDefault
item in the list.This will ensure that the first item is always selected. You do not need to implement
INotifyPropertyChanged
on theSelectedObject
property unless you are planning on manually changing the value of this property in your View Model.