带有null值的Combobox - SelectedItem绑定 [英] Combobox with null value - SelectedItem binding

查看:281
本文介绍了带有null值的Combobox - SelectedItem绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带城市选项的combobox选择,其中一个选项是空选项(没有城市)。 itemsource绑定到城市对象的列表。列表包含空值以表示空选项。 SelectedItem绑定到城市类型的属性。一切工作除了在组合框中选择空选项的情况。绑定到SelectedItem的属性不使用空值更新,但保留之前的选择。

解决方案



谢谢asnwer
Greg

/ div>

这不是我的经验。我有以下XAML:

 < TextBlock Text ={Binding ElementName = _cbx,Path = SelectedItem}Margin = 20/> 
< ComboBox x:Name =_ cbxItemsSource ={Binding Cities}Horizo​​ntalAlignment =LeftSelectionChanged =OnNewCity/>

我在代码隐藏中创建了一个名为Cities的属性为List,并用字符串和null。
当选择null时,SelectedItem是一个ComboBoxItem。



在OnNewCity中,我根据SelectedItem填充另一个文本框, / p>

您可以在XAML和代码上提供更多信息吗?



在作者评论后编辑:



谢谢XAML。我使用了相同的,一个城市类,而不是一个字符串列表,我得到与你一样的行为。 SelectedCity中的断点显示未调用setter。
当City对象为null时,SelectedItem属性的类型为ComboBoxItem,因此我的猜测是WPF寻找一个与ComboBoxItem兼容的类型的SelectedCity属性,以便调用setter。它在这种情况下找不到一个。
我改变了我的代码隐藏设置类型对象的SelectedCity。在这种情况下,调用setter,即使是一个空城!



我不知道改变SelectedCity的类型是一个好办法。类型对象不应该被过度使用。但是你可以有另一个属性只使用绑定(和类型对象),在类型检查后正确设置SelectedCity。



另一个更好的解决方案是考虑它是否感觉把一个空城市放在一个绑定的列表。您可以移除这个城市,或是有一个特殊的城市,其中有一个特殊的名字,代表null?


I would like to have a combobox with cities options to choose, one of the option is an empty option (no city). The itemsource is binded to the List of "City" objects. The List contains null value to represent an empty option. The SelectedItem is binded to a property of "City" type. Everything works except the situation when the empty option is picked in the combobox. The property binded to SelectedItem is not updated with the null value but keeps the previous selection. How could i solve this out?

thank you for asnwer Greg

解决方案

This is not my experience. I have the following XAML:

<TextBlock Text="{Binding ElementName=_cbx, Path=SelectedItem}" Margin="20" />
<ComboBox x:Name="_cbx" ItemsSource="{Binding Cities}" HorizontalAlignment="Left" SelectionChanged="OnNewCity" />

I created a properties called Cities in the code-behind as List and filled it with values that are strings and nulls. When null is selected, the SelectedItem is a ComboBoxItem.

In the OnNewCity, I populate another text box based on SelectedItem and I see the same behaviour.

Could you give more info on your XAML and code ?

Edit after author's comment:

Thank you for the XAML. I used the same one, with a City class instead of a list of strings and I get the same behaviour as you do. A breakpoint in SelectedCity shows that the setter is not called. When the City object is null, the SelectedItem property is of type ComboBoxItem and thus my guess is that WPF looks for a SelectedCity property of a type compatible with ComboBoxItem in order to call the setter. It cannot find one in this case. I changed my code-behind to set SelectedCity of type object. In this case, the setter is called, even for a null city!

I am not sure that changing the type of SelectedCity is a good way to go. Type object should not be overused. But you could have another property used only in binding (and of type object) that sets the SelectedCity correctly after type checking.

Another, better solution is to consider whether it makes sense to put a null city in a bound list. Could you remove this or have a special city with a special name that would represent null ?

这篇关于带有null值的Combobox - SelectedItem绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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