WPF 组合框选定值在更新选定项时不更新
有绑定到组合框的 Observable 集合。
public ObservableCollection<AnyType> AnyTemplates { get; set; }
和绑定到该集合的组合框:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding Path=DataContext.KeyTemplates, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"/>
第一个集合是空的。然后,当我在集合中添加新值时,复选框 selectedItem 更改为该值。如果我更改集合项中的名称属性,则组合框选定项会更改(我看到 DisplayMemberPath 更改为新值),但选定值不会更改,直到我再次手动选择此项目。 Name属性集合元素调用PropertyChanged事件。 为什么这不起作用。
摘要:当我以编程方式更改组合框 SelectedItem 中的 NameProperty 时,组合框 SelectedItem 已更改,但 SelectedValue 不会更新,直到我再次在组合框中手动更改它。
There is Observable collection which bind to combobox.
public ObservableCollection<AnyType> AnyTemplates { get; set; }
And combobox which bind to this collection:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding Path=DataContext.KeyTemplates, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"/>
First collection is empty. Then when i add new value in collection, checkBox selectedItem change to this value. If I change Name property in collection Item, combobox selectedItem is changed(I see what DisplayMemberPath change to new value), but Selected value not changed until i manualy choose this item again.
The Name property collection element call PropertyChanged event.
Why this did not work.
Summary: when I change NameProperty in comboxo SelectedItem programicaly, combobox SelectedItem is changed, but SelectedValue not update until i manualy change it in combobox again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 ComboBox 的 ItemStyle 容器,使其看起来像这样:
另外,请确保您已使用 NotifyPropertyChanged 完成所有操作并设置 DataContext。另一件不要做的事情是确保首先在加载时在视图模型中设置初始值,然后只有 SelectedItem 会更改。
Try using the ItemStyle Container for the ComboBox so it looks like this:
Also, make sure that you have done everything with NotifyPropertyChanged and set up the DataContext. Another thing to not is to make sure you set up the initial values in the view model on load first and then just the SelectedItem will change.