WPF - 组合框 SelectedItem 未设置?
我有一个 ComboBox,其 ItemsSource 绑定到静态 List
选项。 ComboBox 是绑定到 CustomObject 类的表单的一部分,该类的属性之一是 CustomSettingProperty。
我想将 ComboBox 的 SelectedItem 绑定到 CustomObject 中指定的属性,但是 SelectedItem="{Binding Path=CustomSettingProperty}"
没有设置默认选定的项目。使用断点我可以看到它正在调用 get;
方法,所以我认为问题可能在于 CustomSettingProperty 是与 List
分开创建的,所以WPF 认为这不是同一个项目。
有没有简单的方法可以做到这一点?或者也许是替代方案,因为 CustomSettings 类确实包含 Id?
I have a ComboBox that has its ItemsSource bound to a static List<CustomSettings>
of options. The ComboBox is part of a form which is bound to a CustomObject class, and one of the properties on that class is a CustomSettingProperty.
I would like to bind the SelectedItem of the ComboBox to the property specified in the CustomObject, however SelectedItem="{Binding Path=CustomSettingProperty}"
is not setting the default selected item. Using breakpoints I can see that it is calling the get;
method, so I think the problem might be in the fact the CustomSettingProperty is created separately from the List<CustomObject>
so WPF does not think it is the same item.
Is there an easy way to do this? Or perhaps an alternative since the CustomSettings class does contain an Id?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果所选项目与列表中包含的实例不同,则必须重写 CustomObject 中的 Equals() 以使 ComboBox 知道它是同一对象。
如果是同一个实例,也许只是简单的事情,例如将 BindingMode 设置为 TwoWay:
If the item that is selected is not the same instance that is contained in the List, you must override Equals() in the CustomObject to let the ComboBox know that it is the same object.
If it's the same instance, maybe it's only a simple thing such as setting the BindingMode to TwoWay:
我找到了解决方案,这是 Prism 的事件聚合器通过引用类型传递,以便 ui 线程停止处理
I found the solution, It was The Prism's Event Aggregator was passed with reference type so That the ui thread stops processing