.netwpfxamltargetnullvalue

DataTemplate with TargetNullValue in a ListBox


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.


Solution

  • If you are looking for a solution, maybe you can find what you want here: Why can't I select a null value in a ComboBox?

    Combobox has the same behavior as ListBox.