WPF - 组合框 SelectedItem 未设置?

发布于 2024-09-14 22:27:24 字数 509 浏览 10 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-09-21 22:27:24

如果所选项目与列表中包含的实例不同,则必须重写 CustomObject 中的 Equals() 以使 ComboBox 知道它是同一对象。

如果是同一个实例,也许只是简单的事情,例如将 BindingMode 设置为 TwoWay:

SelectedItem="{Binding Path=CustomSettingProperty,Mode=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:

SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"
对岸观火 2024-09-21 22:27:24

我找到了解决方案,这是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文