Null items not shown in ComboBox with binding
I'm having some difficulties with null and a ComboBox in WPF/C#; If a add a null item it is not shown/listed in the ComboBox.
开发者_C百科Here is de XAML declaration of my ComboBox
<ComboBox ItemsSource="{Binding Path=MyList}"
SelectedItem="{Binding Path=Type}" />
MyList is declared as
public ObservableCollection<Type> MyList { get; private set; }
and initialized with
this.MyList = new ObservableCollection<Type>();
this.MyList.Add(null);
The binding works fine, and if I add non-null items the are listed. Do I need to specify to allow null values?
lg, Dominik
You can try using the TargetNullValue property in your binding, setting some default. I.e.,
{Binding Path=MyList, TargetNullValue="Empty Item"}
A ComboBox
like every item i've encountered cannot render anything for a null item. It's my understanding that if there is no visual content for it to render, it calls the ToString() method on the item and renders that. As your item is null, this is not possible.
I think this Q & A might be helpful to you though.
Display a default DataTemplate in a ContentControl when its content is null or empty?
精彩评论