列表框中具有 TargetNullValue 的 DataTemplate
我在列表框中有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找解决方案,也许您可以在这里找到您想要的:
为什么我不能在组合框中选择空值?
组合框与列表框具有相同的行为。
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.
下面的博客提供了一个使用附加属性的良好解决方案,该解决方案对我来说效果很好
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
这就是我认为正在发生的事情:-
我假设 ItemSource 是裸字符串值的简单集合(即未封装在另一个类中)。 当您在对象上按下鼠标按钮时,隐藏代码会将集合中项目的对象引用复制到列表框的 SelectedItem 字段。
因此,如果集合是:-“Fred”、null、“Jane”、“Mary”,并且您在“Fred”上按鼠标,则“Fred”的对象引用将复制到 SelectedItem。 如果按第二个项目,该对象引用 (null) 将被复制到 SelectedItem。
问题是 SelectedItem 中的 NULL 值实际上意味着没有选择任何项目的特殊情况。
即使在 TargetNullValue 属性中指定了“None”,您也不会将其复制到 SelectedItem。 这只是集合元素包含 NULL 值时的视觉表示。 列表框仅对集合的对象引用感兴趣,而不对 UI 中显示的内容感兴趣。
解决此问题的一种方法是使用名为“name”的字符串字段创建一个非空对象集合。
例如,
这意味着 List 中的任何项目都不会具有 NULL 值。
然后在 DataTemplate 的绑定中使用: -
即使名称为 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.
This will then mean that no item in List will have a NULL value.
Then in the Binding of the DataTemplate use:-
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.