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.
您可能想要使用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.