在一个TreeView任何项目OnExpanded事件 [英] OnExpanded event for any item in a treeview

查看:96
本文介绍了在一个TreeView任何项目OnExpanded事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个事件的树型视图在我的树视图任何扩大。

I'd like to get an event for any expansion of a treeviewitem in my treeview.

这样做的原因,有点无关的原题:
我创建密切相关的XML文件树一棵树,但我允许一个包括XML元素使树可以在多个文件中去。我想设置在扩张时我的树视图中treeviewitems的ItemsSource属性。

The reason for this, a bit unrelated to the original question: I am creating a tree that relates closely to an xml file tree, but I am allowing an include element in the xml so the tree can go across multiple files. I'd like to set the itemssource property of treeviewitems within my treeview upon expansion.

推荐答案

您可以使用 TreeViewItem.Expanded 事件作为一个附加的事件:

You can use the TreeViewItem.Expanded event as an attached event :

<TreeView TreeViewItem.Expanded="TreeViewItem_Expanded"
          ItemsSource="{Binding}">
    ...
</TreeView/>

在代码隐藏,可以识别树型视图发起使用 OriginalSource 属性在事件:

In code-behind, you can identify the TreeViewItem that initiated the event using the OriginalSource property :

    private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
    {
        TreeViewItem tvi = e.OriginalSource as TreeViewItem;
        if (tvi != null)
        {
            MessageBox.Show(string.Format("TreeNode '{0}' was expanded", tvi.Header));
        }
    }

这篇关于在一个TreeView任何项目OnExpanded事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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