views:

1220

answers:

2

I have the following DataTemplate in a Listbox

<ListBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}">
<ListBox.ItemTemplate>
 <DataTemplate>
  <TextBlock OverridesDefaultStyle="True"
      Background="{x:Null}"
      Margin="0"
      Padding="0"
      IsHitTestVisible="True"
      Text="{Binding TargetNullValue=None}"
  />
 </DataTemplate>
</ListBox.ItemTemplate>

This works perfectly, displaying "None" in place of any Null (Nothing) values in the bound list. The problem is that I can't click on the Null values to select them. Selection with the keyboard works perfectly, just not with a mouse. What can I do to make the Null values in the list act just like any other value?

Edit: I should also add that I can change the TextBlock's background to Red and it displays just like the others so I don't think it's a case of having nothing to click on. I've also looked at it with Snoop and I don't see any attributes in the visual tree that are different between a Null item and a normal item.

Edit 2: I should add that People is actually a class representing a database table. It uses the ToString method to display the People objects by default. I get the same effect if I bind to the proper field using the Path option and I thought this would be easier to read.

A: 

This is what I think is happening:-

I'm assuming the ItemSource is a simple collection of bare strings values(i.e. not encapsulated in another class). When you press the mouse button on an object the code-behind copies the object reference of the item in the collection to the SelectedItem field of the list box.

so if the collection is :- "Fred", null, "Jane", "Mary" and you press the mouse on "Fred" then the object reference of "Fred" is copied to SelectedItem. If you press on the second item, that object reference (null) is copied to SelectedItem.

The problem is that a value of NULL in SelectedItem actually means a special case where no item is selected.

You will not get "None" copied to SelectedItem even though it is specified in your TargetNullValue attribute. This is just a visual representation when the collection element contains the NULL value. The listbox is only interested in the object references of the collection, not what is shown in the UI.

The one way around this is to create a non-null collection of objects with a string field called "name".

e.g.

class People
{
   string Name {get;set;}  
}

...
...

var list = new List<People> {new People {Name = "Fred"},
                             new People {Name = null},
                             new People {Name = "Jane"},
                             };

This will then mean that no item in List will have a NULL value.

Then in the Binding of the DataTemplate use:-

Text="{Binding Path=Name, TargetNullValue=None}"

The SelectedItem for each element will now be non-NULL even if the name is NULL but the drawback for you is that the SelectedItem is now no-longer a string of the name selected but a reference to the People object that was selected.

Rhys
Unfortunately I am already binding to a more complex object. So it isn't a nullable problem like you suggest. Thanks for the idea, I will update my question.
Bryan Anderson
+1  A: 

If you are looking for a solution, maybe you can find what you want here: http://stackoverflow.com/questions/518579/why-cant-i-select-a-null-value-in-a-combobox

Combobox has the same behavior as ListBox.

redjackwong
Thank you. I had noticed this question when it was originally posted and commented to them that the answers would be the same.
Bryan Anderson