问题
I have seen and tested a lot of material about this and I have the problem again. I have a collection of cities bound to the combobox and I want to set the selected item (selected city) when I click the button (for simplicity)
this is my combobox in xaml:
<ComboBox Grid.Column="1"
HorizontalAlignment="Left"
Margin="65,10,0,0"
Height="25"
Name="Cities"
DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True"
VerticalAlignment="Top"
Width="71">
</ComboBox>
this is code in corresponding ViewModel:
private City _selectedCity;
public City SelectedCity
{
get { return _selectedCity; }
set
{
_selectedCity = value;
NotifyOfPropertyChange(() => SelectedCity);
MessageBox.Show(SelectedCity.Name);
}
}
When I click the button this line of code is executed:
SelectedCity = CitySecond;
where, CitySecond is one random item from the Cities Collection.
When I click this button the desired city name appears in MessageBox, but nothing changes in combobox. I tested it in Debug mode and everything looks fine, but as it seems comboBox doesn't feels that something has changed.
I have also added the following line of code in combobox xaml definition but nothing changed:
SelectedItem="{Binding SelectedCity}"
What Can I do?
EDIT:
I have created another project, where everything works fine, but here is mystery. Does anything other affects the combobox to work properly?
来源:https://stackoverflow.com/questions/51498387/bind-selected-item-to-combobox-with-caliburn-micro