列表框中具有 TargetNullValue 的 DataTemplate

发布于 2024-07-11 13:44:15 字数 950 浏览 6 评论 0原文

我在列表框中有以下 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>

这完美地工作,显示“无”来代替绑定列表中的任何空(无)值。 问题是我无法单击空值来选择它们。 使用键盘进行选择效果很好,但使用鼠标则不行。 我该怎么做才能使列表中的 Null 值像任何其他值一样起作用?

编辑:我还应该补充一点,我可以将 TextBlock 的背景更改为红色,并且它的显示方式与其他背景一样,因此我不认为这是没有任何可单击的情况。 我还用 Snoop 查看过它,并且在可视化树中没有看到 Null 项目和普通项目之间有任何不同的属性。

编辑2:我应该补充一点,People 实际上是一个代表数据库表的类。 默认情况下,它使用 ToString 方法显示 People 对象。 如果使用“路径”选项绑定到正确的字段,我会得到相同的效果,并且我认为这会更容易阅读。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑红尘 2024-07-18 13:44:15

如果您正在寻找解决方案,也许您可​​以在这里找到您想要的:
为什么我不能在组合框中选择空值?

组合框与列表框具有相同的行为。

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.

牛↙奶布丁 2024-07-18 13:44:15

下面的博客提供了一个使用附加属性的良好解决方案,该解决方案对我来说效果很好

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

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

暖伴 2024-07-18 13:44:15

这就是我认为正在发生的事情:-

我假设 ItemSource 是裸字符串值的简单集合(即未封装在另一个类中)。 当您在对象上按下鼠标按钮时,隐藏代码会将集合中项目的对象引用复制到列表框的 SelectedItem 字段。

因此,如果集合是:-“Fred”、null、“Jane”、“Mary”,并且您在“Fred”上按鼠标,则“Fred”的对象引用将复制到 SelectedItem。 如果按第二个项目,该对象引用 (null) 将被复制到 SelectedItem。

问题是 SelectedItem 中的 NULL 值实际上意味着没有选择任何项目的特殊情况。

即使在 TargetNullValue 属性中指定了“None”,您也不会将其复制到 SelectedItem。 这只是集合元素包含 NULL 值时的视觉表示。 列表框仅对集合的对象引用感兴趣,而不对 UI 中显示的内容感兴趣。

解决此问题的一种方法是使用名为“name”的字符串字段创建一个非空对象集合。

例如,

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

...
...

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

这意味着 List 中的任何项目都不会具有 NULL 值。

然后在 DataTemplate 的绑定中使用: -

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

即使名称为 NULL,每个元素的 SelectedItem 现在也将为非 NULL,但缺点是 SelectedItem 现在不再是所选名称的字符串,而是引用到所选的人员对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文