开发者

Update shown field in combobox DisplayMemberPath not shown

I have a wpf combobox. Its ItemsSource has binding to an ObservebaleCollection. The shown value (via DisplayMemberPath) is the Name property of the Entity class. The problem is when I update the current selected entity name and fire NotifyPropertyChnage it is not updated in the UI (even so that when I open the combo list it is updated there). I guess the problem is that the entity hashcode is still the same and the combo doesn't see a difference. what can I do?

xaml:

<ComboBox     ItemsSource="{Binding Entities, Mode=OneWay}" 
          SelectedItem="{Binding CurrentEntity}"
          DisplayMemberPath="Name"/>

code:

    public event PropertyChangedEventHandler PropertyChanged;

    ObservableCollection<Entity> m_e开发者_Go百科ntities = new ObservableCollection<Entity>();

    public ObservableCollection<Entity> Entities{get{return m_entities;}} 

    public Entity CurrentEntity{get;set}

    public void RenameEntity(string name)
    {
    m_currentEntity.Name = name;
    PropertyChanged(this, new PropertyChangedEventArgs("CurrentEntity"));
    PropertyChanged(this, new PropertyChangedEventArgs("Entities"));
    }


Apparently, the problem is that a combobox calls ToString on the data object to display the selected item and uses DisplayMemberPath for items in the drop-down.

To fix this, use a DataTemplate instead of DisplayMemberPath:

<DataTemplate x:Key="EntityTemplate"
              DataType="{x:Type my:Entity}">
    <TextBlock Text="{Binding Name}"/>
</DataTemplate>

And assign it to the combobox's ItemTemplate property:

<ComboBox ItemsSource="{Binding Entities}"
          ItemTemplate="{StaticResource EntityTemplate}"
          SelectedItem="{Binding ...}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜