Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...