WPF;单击ListView中的空白区域时如何取消选择所有选中的项目 [英] WPF; How Deselect all my selected items when click on empty space in my ListView

查看:326
本文介绍了WPF;单击ListView中的空白区域时如何取消选择所有选中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有几个(或什至一个)selected items时,我在ListView中的空白处按简单的click(空白处=不行),我想取消选择所有选中的项.

When i have several (or even one) selected items and i press simple click on empty space in my ListView (empty space = not row) i want to deselect all my selected items.

这是我取消选择所有项目的功能:

This is my deselect all item function:

private void DeselectAllListViewItems()
{
    MyListView.SelectedItems.Clear();
} 

我尝试使用此功能获取选定的索引:

I try to take the selected index with this function:

private void MyListView_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (MyListView.SelectedIndex == -1)
        DeselectAllListViewItems();
}

但是如果我有几个选定的项目(或一个..),则选定的索引永远不会为-1. 那么我如何区分我的mouse click是在空白处而不是在项目行中?

But in case i have several selected items (or one..) the selected index will never be -1. So how can i distinguish that my mouse click is on empty space and not on item row ?

推荐答案

下面的代码效果很好.

private void MyListView_MouseDown(object sender, MouseButtonEventArgs e)
{
    HitTestResult r = VisualTreeHelper.HitTest(this, e.GetPosition(this));
    if (r.VisualHit.GetType() != typeof(ListBoxItem))
        listView1.UnselectAll();
}

WPF列表框通过单击空白来删除选择点

这篇关于WPF;单击ListView中的空白区域时如何取消选择所有选中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆