为什么 e.Source 依赖于 TreeView 填充方法? [英] Why e.Source depends on TreeView populating method?
问题描述
我有两棵树:
- fooTree - 由元素组成,
- barTree - 由 构建
两棵树都有 MouseRightButtonDown 事件,但 e.Source 类型不同:
- fooTree - System.Windows.Controls.TreeViewItem
- barTree - System.Windows.Controls.TreeView
为什么 e.Source 不同?另外,如何获取 barTree 的点击项?
标记:
<TreeView Name="fooTree" MouseRightButtonDown="fooTree_MouseDown"><TreeViewItem Header="foo"></TreeViewItem><TreeViewItem Header="foo"></TreeViewItem></树视图><TreeView 名称="barTree" MouseRightButtonDown="barTree_MouseDown" ItemsSource="{Binding BarItems}"><TreeView.ItemTemplate><分层数据模板><TextBlock 文本="{绑定}"/></分层数据模板></TreeView.ItemTemplate></树视图>
代码:
public 部分类 Window1 : Window{公共窗口1(){初始化组件();this.DataContext = this;}公共字符串[] BarItems{获取 { 返回新字符串 [] { "bar", "bar" };}}私人无效barTree_MouseDown(对象发送者,MouseButtonEventArgs e){}私人无效fooTree_MouseDown(对象发送者,MouseButtonEventArgs e){}}
不知道为什么会这样,但至少我找到了解决方案:
http:///social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0d3af69-6ecc-4ddb-9526-588b72d5196b/
<块引用>如果您的处理程序位于 TreeView 上,请使用事件参数并走上视觉父链,直到你找到一个树视图项.然后,选择它.你可以通过使用走可视父链System.Windows.Media.VisualTreeHelper.GetParent.
您可以尝试为 TreeViewItem 类型注册一个类处理程序和鼠标按下事件.然后,您的处理程序只应在鼠标时调用事件通过 TreeViewItem元素.
您可以为 TreeViewItem 类型和上下文注册一个类处理程序菜单打开事件.
所以我的代码是:
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e){TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;}static DependencyObject VisualUpwardSearch(DependencyObject 源){while (source != null && source.GetType() != typeof(T))source = VisualTreeHelper.GetParent(source);返回源;}
I have two trees:
- fooTree - made up of elements,
- barTree - constructed by
Both trees have MouseRightButtonDown event, but the e.Source type differs:
- fooTree - System.Windows.Controls.TreeViewItem
- barTree - System.Windows.Controls.TreeView
Why e.Source differs? Also, how can I get the clicked item for the barTree?
Markup:
<TreeView Name="fooTree" MouseRightButtonDown="fooTree_MouseDown">
<TreeViewItem Header="foo"></TreeViewItem>
<TreeViewItem Header="foo"></TreeViewItem>
</TreeView>
<TreeView Name="barTree" MouseRightButtonDown="barTree_MouseDown" ItemsSource="{Binding BarItems}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Code:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = this;
}
public string[] BarItems
{
get { return new string[] { "bar", "bar" }; }
}
private void barTree_MouseDown(object sender, MouseButtonEventArgs e)
{
}
private void fooTree_MouseDown(object sender, MouseButtonEventArgs e)
{
}
}
Don't know why this happens, but at least I have found a solution:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0d3af69-6ecc-4ddb-9526-588b72d5196b/
If your handler is on the TreeView, use the OriginalSource property in the event arguments and walk up the visual parent chain until you find a TreeViewItem. Then, select it. You can walk the visual parent chain by using System.Windows.Media.VisualTreeHelper.GetParent.
You could try registering a class handler for type TreeViewItem and the mouse down event. Then, your handler should only be called when mouse events pass through TreeViewItem elements.
You could register a class handler for type TreeViewItem and the context menu opening event.
So my code is:
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
}
static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
{
while (source != null && source.GetType() != typeof(T))
source = VisualTreeHelper.GetParent(source);
return source;
}
这篇关于为什么 e.Source 依赖于 TreeView 填充方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!