WPF列表框通过单击空白区域删除选择

13

本文介绍了WPF列表框通过单击空白区域删除选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个带有自定义项目模板的 wpf listbox,其中包含一个矩形.listbox 中的每一项都可以选择(一次只能选择一项).我想添加一种行为,当用户单击不是项目的地方(例如,listbox 上的空白点,它不是项目)时,所选项目将被取消选中.

I have a wpf listbox with a custom item template which contains a rectangle. The each item in the listbox can be selected (only one at a time). I want to add a behavior in which when a user clicks on a place which isn't the item (for instance, a blank spot on the listbox, which is not an item), the selected item will become deselected.

有什么想法吗?谢谢.

以一个简单的列表框为例:第 1 项第 2 项

For example with a simple listbox: item 1 item 2

我正在寻找的行为是当用户点击像素 500(这是 listbox 的一部分但不是项目的一部分)时,当前选定的项目将被取消选择.

The behavior that I'm looking for is when the user clicks on pixel 500 (which is a part of the listbox but not on an item), the currently selected item will be deselected.

推荐答案

简单的解决方案是将属性数据绑定到 ListBox.SelectedItem 属性并将其设置为 null 每当您想清除选择时:

The simple solution is to data bind a property to the ListBox.SelectedItem property and set it to null whenever you want to clear the selection:

<ListBox ItemsSource="{Binding YourItems}" SelectedItem="{Binding SelectedItem}" 
    SelectionMode="Single" />

然后在代码中,你可以这样做来清除选择:

Then in code, you can just do this to clear the selection:

SelectedItem = null;

你什么时候会这样做?您可以将处理程序附加到 Window 或 UI 中的任何其他控件的 ">PreviewMouseLeftButtonDown 事件.在处理程序方法中,您可以进行命中测试以查看用户单击的项目是什么:

And when would you do that? You can attach a handler to the PreviewMouseLeftButtonDown event of the Window, or any other control in your UI. In the handler method, you could do a hit test to see what the item the user clicked on was:

HitTestResult hitTestResult = 
    VisualTreeHelper.HitTest(controlClickedOn, e.GetPosition(controlClickedOn));
Control controlUnderMouse = hitTestResult.VisualHit.GetParentOfType<Control>();

请参阅 VisualTreeHelper.HitTestMethod (Visual, Point) 获取更多关于这部分的帮助.

See the VisualTreeHelper.HitTest Method (Visual, Point) for more help with this part.

然后可能是这样的:

if (controlUnderMouse.GetType() != typeof(ListBoxItem)) SelectedItem = null;

当然,有很多方法可以做到这一点,你必须填补我留下的几个空白,但你应该明白.

Of course, there are many ways to do this, and you'll have to fill in the few blank spots that I left, but you should get the idea.

编辑>>>

通用 GetParentOfType 方法是自定义 扩展方法,在一个名为 DependencyObjectExtensions 的单独类中定义:

The generic GetParentOfType method is a custom Extension Method that is defined in a separate class named DependencyObjectExtensions:

public static class DependencyObjectExtensions
{
    public static T GetParentOfType<T>(this DependencyObject element) 
        where T : DependencyObject
    {
        Type type = typeof(T);
        if (element == null) return null;
        DependencyObject parent = VisualTreeHelper.GetParent(element);
        if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) 
            parent = ((FrameworkElement)element).Parent;
        if (parent == null) return null;
        else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) 
            return parent as T;
        return GetParentOfType<T>(parent);
    }

    ...
}

这篇关于WPF列表框通过单击空白区域删除选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14