Bind Selected Item to comboBox with caliburn micro

 ̄綄美尐妖づ 提交于 2019-12-13 00:17:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!