绑定更改时 WPF 组合框文本框不更新
我有一个 WPF CombBox,如下所示:
<ComboBox
ItemsSource="{Binding Source={StaticResource myList}}"
SelectedItem="{Binding Path=mySelectedItem}"
/>
我遇到的问题是,当绑定值更改时,组合框文本框中选定的值不会更新。 (注意 - 组合框列表中的值会更新)。
我正在使用 MVVM,因此我可以在视图模型中检测绑定何时更改并调用属性更改事件,这会更新组合框,但不会更新文本框中显示的值。
我认为这可以在组合框的模板中完成 - 以某种方式使文本框绑定到组合框的选定项,或者在更新时始终更新?
编辑:
我没有说清楚 - 我确实正确实现了 INotifyPropertyChanged,当值更改时,我为 myList 和 mySelectedItem 引发 PropertyChanged。问题是组合框中的文本框不刷新。
我发现了一个类似的问题:
WPF ComboBox SelectedItem not Updating
这给出了答案的提示,但是不幸的是还不够。
I have a WPF CombBox as follows:
<ComboBox
ItemsSource="{Binding Source={StaticResource myList}}"
SelectedItem="{Binding Path=mySelectedItem}"
/>
The problem I have is that when the bound value changes, the selected value in the combobox's textbox does not update. (Note - the values in the combobox list do update).
I am using MVVM so I can detect in the view model when the binding changes and call a property changed event and this is updating the combobox, but not the value displayed within the textbox.
I think this could be done in the template of the combobox - somehow make the textbox be bound to the selecteditem of the combobox, or always update when it updates?
EDIT:
I didn't make clear - I do implement INotifyPropertyChanged properly and when the value changes I raise PropertyChanged for myList and mySelectedItem. The problem is that the textbox within the combobox is not refreshing.
I found a similar question:
WPF ComboBox SelectedItem not Updating
Which gives hints of an answer but not enough unfortunately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
mySelectedItem
很可能不是依赖属性。或者,包含类不实现 INotifyPropertyChanged。考虑您的值既不是依赖属性也不是
INotifyPropertyChanged
的属性的情况。在这种情况下,WPF 没有机会知道其值已更改,因此该值在开始时仅加载一次(使用反射)。对于依赖属性的情况,或者当包含对象支持 INotifyPropertyChanged 时,WPF 框架可以订阅值更改,从而更新组合框。
Most probably your
mySelectedItem
is not a dependency property. Or, alternatively, the containing class doesn't implementINotifyPropertyChanged
.Consider the case when your value is neither a dependency property nor a property of an
INotifyPropertyChanged
. In this case, WPF doesn't have a chance to know that its value changed, therefore the value is loaded only once (using reflection) at the beginning.For the case of dependency property, or when the containing object supports
INotifyPropertyChanged
, the WPF framework can subscribe to the value changes and thus update the combobox.如果您希望当“myList”集合后面的值更改时所选项目会更改,则它不会更改。如果您使用 MVVM 并且正在视图模型中实现 INotifyPropertyChanged。然后,当您为 myList 引发 PropertyChanged 时,如果您希望它以任何方式重新绑定或更改,则还应该为 'mySelectedItem' 引发它。
If your expecting the selected item to change when the value behind your 'myList' collection changes it wont. If your using MVVM and you are implementing INotifyPropertyChanged in the view model. Then when you are raising the PropertyChanged for myList, you should also be raising it for 'mySelectedItem' if you are expecting it to rebind or change in any way.
我已经在此处为您更新了答案
希望有帮助!
I have updated my answer for you here
Hope it helps!!