Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update shown field in combobox DisplayMemberPath not shown

Tags:

.net

combobox

wpf

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"));
    }
like image 507
ie1 Avatar asked Feb 01 '11 08:02

ie1


People also ask

Where do I set the displaymemberpath and selecteditem in combobox?

The DisplayMemberPath in the ComboBox is set to the DisplayName property. The SelectedItem property is set to the employee. We also have three more ComboBoxes on the same page that behave correctly (just binding to different properties) and cause no issue. Any idea what could go wrong? Thank you for the provided details.

What is displaymemberpath and how do I use it?

With DisplayMemberPath it’s even easier for the scenario where you want to display only one property of each data item as text. Before DisplayMemberPath, this scenario required the use of a DataTemplate that would specify the property we’re interested in, like in the following xaml:

How to get the correct person to show up in combobox?

If you want to get the proper person to show up in this field when the PowerApp is opened to that particular item in Edit mode, please take a try with the following workaorund: Set the DefaultSelectedItems property of the ComboBox (on your side, it is cboExecMgr ) within the PersonColumn to following:

How to set the default selected items of a combobox?

Set the DefaultSelectedItems property of the ComboBox (on your side, it is cboExecMgr ) within the PersonColumn to following: { Result: ThisItem. PersonColumn .DisplayName }


1 Answers

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 ...}"/>
like image 172
Pavlo Glazkov Avatar answered Nov 04 '22 01:11

Pavlo Glazkov