如果已选择,则在单击时取消选择 GridView.Item [英] Unselect GridView.Item on click if already selected

查看:25
本文介绍了如果已选择,则在单击时取消选择 GridView.Item的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这看起来很简单,我也是这么想的,但实际上并非如此.我有一个 GridView,SelectionMode="Single",我想通过单击它来简单地取消选择一个选定的项目.问题是,当您选择已选择的项目时,SelectionChanged 不会触发.我尝试在每个 SelectionChanged 上设置一个等于 GridView 的 SelectedIndex 的 int,然后检查 Grid_Tapped 以查看 PreviousSelectedIndex == CurrentSelectedIndex,但是 SelectionChanged 事件在 Grid_Tapped 之前触发了纳秒,因此它不起作用.有什么想法吗?

I know this seems rather simple, and that's what I thought too, but it actually isn't. I have a GridView, with SelectionMode="Single", and I want to simply unselect a selected item by clicking on it. Problem is, SelectionChanged doesn't fire when you select an item that is already selected. I've tried having an int equal to the GridView's SelectedIndex on each SelectionChanged, and then check on Grid_Tapped to see if PreviousSelectedIndex == CurrentlySelectedIndex, but the SelectionChanged event fires nanoseconds before the Grid_Tapped, so it doesn't work. Any ideas?

推荐答案

是的,这是一个有点奇怪的默认行为,你可以使用以下技巧来解决这个问题(有很多方法)

Yes it is a bit weird default behavior, you can do the following trick to solve that (there are many ways)

1.- XAML

<GridView IsItemClickEnabled="True" ItemClick="IconGridView_ItemClick" SelectionMode="Single">

2.- 事件代码:

 private async void IconGridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var gridView = sender as GridView;
        if (e.ClickedItem == gridView.SelectedItem)
        {
            await Task.Delay(100);
            gridView.SelectedItem = null;
        }
    }

如果您稍等片刻,内部事件会保留所选项目,这样就解决了问题,并取消选择了 SelectedItem.

If you do not wait a bit, the internals events keep the selected item, with that way it is solved and the SelectedItem is Deselected.

这篇关于如果已选择,则在单击时取消选择 GridView.Item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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