DataTemplate with TargetNullValue in a ListBox
Asked Answered
C

3

4

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.

Crosshead answered 7/1, 2009 at 21:2 Comment(3)
It seems that the item is not selectable by keyboard as the selection changes to a dashed border instead of the blue background. Very odd.Ran
It does that but if you put a breakpoint in the setter it is getting called so the selection does happen, the listbox just does the selection highlighting differently.Crosshead
I've just run Snoop over the control and I can't tell the difference. All items are ListBoxItems but I can't spot the difference and I just spent a while in Reflector to try see any changes in how the rendering works.Ran
W
2

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.

Whore answered 23/2, 2009 at 5:3 Comment(1)
Thank you. I had noticed this question when it was originally posted and commented to them that the answers would be the same.Crosshead
K
1

The below blog provides a good solution using Attached Property which is working well for me

http://danylaporte.blogspot.com/2008/07/wpf-combobox-and-null-values.html

Krute answered 4/1, 2012 at 9:18 Comment(0)
F
0

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.

Fifi answered 8/1, 2009 at 14:35 Comment(1)
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.Crosshead

© 2022 - 2024 — McMap. All rights reserved.