简体   繁体   English

在ListBox中使用TargetNullValue的DataTemplate

[英]DataTemplate with TargetNullValue in a ListBox

I have the following DataTemplate in a Listbox 我在Listbox中有以下DataTemplate

<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. 这完美地工作,显示“无”代替绑定列表中的任何Null(Nothing)值。 The problem is that I can't click on the Null values to select them. 问题是我无法单击Null值来选择它们。 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? 我可以做些什么来使列表中的Null值与其他任何值一样?

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. 编辑:我还应该补充一点,我可以将TextBlock的背景更改为红色,它显示就像其他人一样,所以我不认为这是一个无需点击的情况。 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. 我也用Snoop看了它,我没有在可视化树中看到Null项和普通项之间有任何不同的属性。

Edit 2: I should add that People is actually a class representing a database table. 编辑2:我应该补充一点,People实际上是一个表示数据库表的类。 It uses the ToString method to display the People objects by default. 默认情况下,它使用ToString方法显示People对象。 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. 如果我使用Path选项绑定到正确的字段,我会得到相同的效果,我认为这将更容易阅读。

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中选择空值?

Combobox has the same behavior as ListBox. Combobox与ListBox具有相同的行为。

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 http://danylaporte.blogspot.com/2008/07/wpf-combobox-and-null-values.html

This is what I think is happening:- 这就是我的想法: -

I'm assuming the ItemSource is a simple collection of bare strings values(ie not encapsulated in another class). 我假设ItemSource是一个简单的裸字符串值集合(即没有封装在另一个类中)。 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. 当您在对象上按下鼠标按钮时,代码隐藏将集合中项目的对象引用复制到列表框的SelectedItem字段。

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. 因此,如果集合是: - “Fred”,null,“Jane”,“Mary”,并且您在“Fred”上按下鼠标,则“Fred”的对象引用将复制到SelectedItem。 If you press on the second item, that object reference (null) is copied to SelectedItem. 如果按第二个项目,则该对象引用(null)将复制到SelectedItem。

The problem is that a value of NULL in SelectedItem actually means a special case where no item is selected. 问题是SelectedItem中的NULL值实际上意味着没有选择项目的特殊情况。

You will not get "None" copied to SelectedItem even though it is specified in your TargetNullValue attribute. 即使在TargetNullValue属性中指定了“None”,也不会将“None”复制到SelectedItem。 This is just a visual representation when the collection element contains the NULL value. 当collection元素包含NULL值时,这只是一个可视化表示。 The listbox is only interested in the object references of the collection, not what is shown in the UI. 列表框仅对集合的对象引用感兴趣,而不是UI中显示的对象引用。

The one way around this is to create a non-null collection of objects with a string field called "name". 解决这个问题的另一种方法是使用名为“name”的字符串字段创建一个非null的对象集合。

eg 例如

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. 这将意味着List中的任何项都不会具有NULL值。

Then in the Binding of the DataTemplate use:- 然后在DataTemplate的Binding中使用: -

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. 即使名称为NULL,每个元素的SelectedItem现在也将为非NULL,但缺点是SelectedItem现在不再是所选名称的字符串,而是对所选People对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM