I have a question about ComboBox regarding binding selected item on reloading list.
class Model : ViewModelBase
{
/// <summary>
/// The <see cref="Title" /> property's name.
/// </summary>
public const string TitlePropertyName = "Title";
private string _title = "";
/// <summary>
/// Sets and gets the Title property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Title
{
get
{
return _title;
}
set
{
if (_title == value)
{
return;
}
RaisePropertyChanging(TitlePropertyName);
_title = value;
RaisePropertyChanged(TitlePropertyName);
}
}
/// <summary>
/// The <see cref="Id" /> property's name.
/// </summary>
public const string idPropertyName = "Id";
private int _myId = 0;
/// <summary>
/// Sets and gets the id property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public int Id
{
get
{
return _myId;
}
set
{
if (_myId == value)
{
return;
}
RaisePropertyChanging(idPropertyName);
_myId = value;
RaisePropertyChanged(idPropertyName);
}
}
}
Binded ViewModel:
class MainViewModel : ViewModelBase
{
public MainViewModel()
{
PopulateCommand = new RelayCommand(() =>
{
Populate();
});
SetCommand = new RelayCommand(() =>
{
Id = 3;
});
}
public void Populate()
{
MyList = new ObservableCollection<Model>(){
new Model(){
Id = 1,
Title = "numer1"
},
new Model(){
Id = 2,
Title = "numer2"
},
new Model(){
Id = 3,
Title = "numer3"
},
new Model(){
Id = 4,
Title = "numer4"
},
};
}
public RelayCommand PopulateCommand { get; private set; }
public RelayCommand SetCommand { get; private set; }
/// <summary>
/// The <see cref="MyList" /> property's name.
/// </summary>
public const string MyListPropertyName = "MyList";
private ObservableCollection<Model> _myList = null;
/// <summary>
/// Sets and gets the MyList property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<Model> MyList
{
get
{
return _myList;
}
set
{
if (_myList == value)
{
return;
}
RaisePropertyChanging(MyListPropertyName);
_myList = value;
RaisePropertyChanged(MyListPropertyName);
}
}
/// <summary>
/// The <see cref="Id" /> property's name.
/// </summary>
public const string IdPropertyName = "Id";
private int _id = 0;
/// <summary>
/// Sets and gets the Id property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public int Id
{
get
{
return _id;
}
set
{
if (_id == value)
{
return;
}
RaisePropertyChanging(IdPropertyName);
_id = value;
RaisePropertyChanged(IdPropertyName);
}
}
}
xaml:
<ComboBox
ItemsSource="{Binding MyList}"
DisplayMemberPath="Title" SelectedValue="{Binding Id, Mode=TwoWay}" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"/>
<Button Content="Populate"
Command="{Binding PopulateCommand}" />
<Button Content="Set"
Command="{Binding SetCommand}" />
<TextBlock Text="{Binding Id}" Width="100"/>
So, populateButton is creating List for combobox binding, setButton sets the id for 3. Then if I populate list again choosen Id in combobox is 1. Shouldn't it be still 3? I mean, shouldn't the combobox set Selected Item for model where id=3.
I also tried without IsSynchronizedWithCurrentItem set to true. Then after reloading the list there is nothing selected in combobox. Is there a way to synchronize selectedItem with Id?
Seems like the list should be initialized first, then I should set the Id, so combobox could update the selected item. But then why if I use setButton first, then populate, I get expected results(selected item is third item)(only with first use)?