WPF - 组合框的SelectedItem没有得到设置? [英] WPF - Combobox SelectedItem not getting set?

查看:183
本文介绍了WPF - 组合框的SelectedItem没有得到设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有它的ItemsSource绑定到一个静态列表℃的组合框; CustomSettings> 选项。组合框是绑定到CustomObject类,并在该类的属性之一是CustomSettingProperty一种形式的一部分。

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.

我想ComboBox的的SelectedItem绑定到CustomObject指定的属性,但是的SelectedItem ={绑定路径= CustomSettingProperty}不设置默认选择的项目。使用断点,我可以看到它调用搞定; 的方法,所以我觉得这个问题可能在CustomSettingProperty从单独创建的事实清单&LT; CustomObject方式&gt; 所以WPF不认为这是同一个项目

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.

有没有一种简单的方法来做到这一点?或许因为CustomSettings类的替代确实包含一个Id?

Is there an easy way to do this? Or perhaps an alternative since the CustomSettings class does contain an Id?

推荐答案

如果所选的项目不包含在该列表中的同一个实例,您必须覆盖在CustomObject的equals(),以便让组合框知道它是同一个对象。

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.

如果这是相同的情况下,也许这只是一个简单的事情,如BindingMode设置为双向:

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}"

这篇关于WPF - 组合框的SelectedItem没有得到设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆