views:

28

answers:

2

Hello,

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.

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

A: 

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.

http://stackoverflow.com/questions/1217254/display-a-default-datatemplate-in-a-contentcontrol-when-its-content-is-null-or-em

Val
+2  A: 

You can try using the TargetNullValue property in your binding, setting some default. I.e.,

 {Binding Path=MyList, TargetNullValue="Empty Item"}
GWLlosa