在组合框中的DisplayMemberPath更新显示领域没有显示 [英] Update shown field in combobox DisplayMemberPath not shown
问题描述
我有一个WPF组合框。其的ItemsSource具有结合到ObservebaleCollection。所显示的值(通过的DisplayMemberPath)是实体类的名称属性。 问题是,当我更新当前选定的实体名称和消防NotifyPropertyChnage它不是在UI更新(即使如此,当我打开下拉列表是有更新)。 我想这个问题是,实体散列code仍然是相同的组合并没有看到有差别。我该怎么办?
XAML:
<组合框的ItemsSource ={结合的实体,模式=单向}
的SelectedItem ={结合CurrentEntity}
的DisplayMemberPath =名称/>
code:
公共事件PropertyChangedEventHandler的PropertyChanged;
的ObservableCollection<实体> m_entities =新的ObservableCollection<实体>();
公众的ObservableCollection<实体>实体{{返回m_entities;}}
公共实体CurrentEntity {获取;集}
公共无效RenameEntity(字符串名称)
{
m_currentEntity.Name =名称;
的PropertyChanged(这一点,新PropertyChangedEventArgs(CurrentEntity));
的PropertyChanged(这一点,新PropertyChangedEventArgs(实体));
}
显然,这个问题是一个组合框调用数据对象的ToString显示所选项目和使用的DisplayMemberPath在下拉菜单项。
要解决这个问题,使用一个DataTemplate代替的DisplayMemberPath:
< DataTemplate中X:关键=EntityTemplate
数据类型={X:键入我:实体}>
< TextBlock的文本={结合名}/>
< / DataTemplate中>
并将其分配给组合框的ItemTemplate属性:
<组合框的ItemsSource ={结合实体}
ItemTemplate中={的StaticResource EntityTemplate}
的SelectedItem ={结合...}/>
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"));
}
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 ...}"/>
这篇关于在组合框中的DisplayMemberPath更新显示领域没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!