TreeView下右击选中节点
来源:程序员人生 发布时间:2014-03-15 08:57:13 阅读次数:2536次
在TreeView中右击节点,selectNode不是右击的节点,需要另外一个事件绑定:
在.net2.0中可以使用:
方法一:在TreeView中加MouseDown事件
private void tvwZD_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point ClickPoint = new Point(e.X, e.Y);
TreeNode CurrentNode = tvwZD.GetNodeAt(ClickPoint);
tvwZD.SelectedNode = CurrentNode;
}
}
方法二:在TreeView中加NodeMouseClick事件
private void tvwZD_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeView tn = (TreeView)sender;
if (e.Button == MouseButtons.Right)
{
tn.SelectedNode = e.Node;
}
}
在WPF 中.Net3.0 3.5以上用
在TreeViewItem中响应PreviewMouseRightButtonDown事件
private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
if (treeViewItem != null)
{
treeViewItem.Focus();
e.Handled = true;
}
}
static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
{
while (source != null && source.GetType() != typeof(T))
source = VisualTreeHelper.GetParent(source);
return source;
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠