I am using an MVVM pattern and have a ComboBox
that binds to properties in the viewmodel like this:
<ComboBox ItemsSource="{Binding Path=ItemCollection}"
SelectedItem="{Binding Path=SelectedItem}">
</ComboBox>
This works fine. In the viewModel I have
private MyData _selectedItem;
public List<MyData> ItemCollection { get; set; }
public MyData SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
RaisePropertyChanged();
}
}
which also works fine. The ItemCollection binds to the ItemSource of the ComboBox and the SelectedItem gets updated when a new item is selected in the ComboBox.
I want to manually change the SelectedItem in specific cases. Like this (I skip null checks for the sake of simplicity):
public MyData SelectedItem
{
get { return _selectedItem; }
set
{
if (value.Name == "Jump to the First item")
_selectedItem = ItemCollection.First();
else
_selectedItem = value;
RaisePropertyChanged();
}
}
This assumes that the type MyData has a string property thats called Name.
The problem is that if the conditional statement is true, the ItemSource of the ComboBox WILL get updated, however the actual visible selection of the the comboBox will not.
To give some context the comboBox actually binds to a CompositeCollection where there is one item that is styled as a button, so when clicked a dialog box is opened and the result of the dialogresult is determining what item in the comboBox should be selected.. -- Just no matter what I do the "Button" will always stay selected.