简体   繁体   中英

How to detect a left/right click event target in ListView in UWP?

Here I got a ListView. I need to take different operations when the user left/right click on an item or the white space without any items. So how to know the click event is happening on an item or white space ? Where should I handle it in the ListView's events ?

You cant use ListView ItemClick event to get whether is a right or left button Click.

But you can AddHandle in the ListViewItem.

If you have a Grid in ListViewItem that you can add handle to get the mouse down.

    <ListView ItemsSource="{x:Bind AvaloniaCol}" IsItemClickEnabled="True" ItemClick="ListViewBase_OnItemClick">
        <ListView.ItemTemplate>
            <DataTemplate>
               <Grid Background="#FFFFFF" PointerPressed="UIElement_OnPointerPressed">
                    <TextBlock Text="{Binding}"></TextBlock>
               </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

You can write the UIElement_OnPointerPressed in cs.

    private void UIElement_OnPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
        {
            var p = e.GetCurrentPoint((UIElement)sender);
            if (p.Properties.IsLeftButtonPressed)
            {

            }
            else if (p.Properties.IsRightButtonPressed)
            {

            }
        }
    }

You can use var p = e.GetCurrentPoint((UIElement)sender); and p.Properties to get whether is left or right button click.

See http://lindexi.oschina.io/lindexi//post/win10-uwp-%E8%8E%B7%E5%8F%96%E6%8C%89%E9%92%AE%E9%BC%A0%E6%A0%87%E5%B7%A6%E9%94%AE%E6%8C%89%E4%B8%8B/

您可能想要使用UIElement.ContextRequested事件,该事件适用于鼠标和触摸。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM