WPF列表视图中单击鼠标右键问题 [英] wpf listview right-click problem

查看:251
本文介绍了WPF列表视图中单击鼠标右键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我附上一个上下文菜单(右键菜单),以一个WPF列表视图。

so I have attached a context menu (right-click menu) to a wpf listview.

不幸的是,当你右键点击它带来了菜单和同时选择的你是什么以上的项目。有没有办法切断这种右击选中的行为,同时还允许在上下文菜单?

unfortunately, when you right-click it brings up both the menu and selects whatever item you are over. Is there a way to shut off this right-click select behavior while still allowing the context menu?

推荐答案

关键是设定在正确的位置的previewMouseRightButtonDown事件。正如你会发现,即使没有一个文本菜单右键点击一个ListViewItem的将选择的项目,所以我们需要设置事件在每个项目上,而不是在ListView。

The key is setting the PreviewMouseRightButtonDown event in the correct place. As you'll notice, even without a ContextMenu right clicking on a ListViewItem will select that item, and so we need to set the event on each item, not on the ListView.

<ListView>
	<ListView.ItemContainerStyle>
		<Style TargetType="{x:Type ListViewItem}">
			<EventSetter Event="PreviewMouseRightButtonDown"
						 Handler="OnListViewItemPreviewMouseRightButtonDown" />
		</Style>
	</ListView.ItemContainerStyle>
	<ListView.ContextMenu>
		<ContextMenu>
			<MenuItem Header="Menu Item">Item 1</MenuItem>
			<MenuItem Header="Menu Item">Item 2</MenuItem>
		</ContextMenu>
	</ListView.ContextMenu>
	<ListViewItem>Item</ListViewItem>
	<ListViewItem>Item</ListViewItem>
	<ListViewItem>Item</ListViewItem>
	<ListViewItem>Item</ListViewItem>
	<ListViewItem>Item</ListViewItem>
	<ListViewItem>Item</ListViewItem>
</ListView>


private void OnListViewItemPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
	Trace.WriteLine("Preview MouseRightButtonDown");

	e.Handled = true;
}

由于preVIEW事件隧道这将发生在阻止RightMouseButtonDown被选中的ListViewItems $ p $从pventing他们,但不是prevent的RightMouseButtonDown的ListView和静得让文本菜单打开。

Since the preview events are tunneling this will block the RightMouseButtonDown from occurring on the ListViewItems preventing them from being selected, but not prevent the RightMouseButtonDown on the ListView and so still allow the ContextMenu to open.

这篇关于WPF列表视图中单击鼠标右键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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