5
votes

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_entities = 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"));
    }
1
First of all you could post some code :)H.B.
I've had the same problem. I've forced it to refresh by clearing and reassigning the DisplayMemberPath in the code behind right after the item's name (which is pointed by the DisplayMemberPath) was changed. I know it's stupid solution, but it's the easiest one I think.P.W.

1 Answers

7
votes

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 ...}"/>