Wpf-如何获取触发 TreeViewItem.Loaded 事件的 TreeViewItem?

发布于 2024-09-16 04:38:32 字数 1429 浏览 5 评论 0原文

我试图在加载时将一些处理程序附加到每个 TreeViewItem ,然后在卸载时删除处理程序。

这是我的自定义控件中继承自 TreeView 的代码:

public ModdedTreeView()
    {
        this.AddHandler(TreeViewItem.LoadedEvent, new RoutedEventHandler(ItemLoaded));            

        this.AddHandler(TreeViewItem.UnloadedEvent, new RoutedEventHandler(ItemUnloaded));
    }

    protected void ItemLoaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;

        if (item == null)
            return;

        item.AddHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));

        item.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));

        item.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
    }


    protected void ItemUnloaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;

        if (item == null)
            return;

        item.RemoveHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));

        item.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));

        item.RemoveHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
    }

编辑:

我仍然不知道发生了什么。它似乎只是拾取 TreeView 加载事件而不是 TreeViewItem 加载事件。

I am trying to attach some handlers to each TreeViewItem as they are loaded and then remove the handlers as they are unloaded.

Here is the Code that I have in my custom control which inherits from TreeView:

public ModdedTreeView()
    {
        this.AddHandler(TreeViewItem.LoadedEvent, new RoutedEventHandler(ItemLoaded));            

        this.AddHandler(TreeViewItem.UnloadedEvent, new RoutedEventHandler(ItemUnloaded));
    }

    protected void ItemLoaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;

        if (item == null)
            return;

        item.AddHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));

        item.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));

        item.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
    }


    protected void ItemUnloaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;

        if (item == null)
            return;

        item.RemoveHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));

        item.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));

        item.RemoveHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
    }

Edit:

I still can not figure out what is going on. It just seems to be picking up the TreeView loaded event instead of the TreeViewItem loaded event.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-09-23 04:38:32

Loaded 是一个路由事件,因此您可以在 OriginalSource 属性中找到其来源,该属性应该是触发该事件的 TreeViewItem。

Loaded is a routed event, so you will find its source in the OriginalSource property, which should be the treeviewitem that triggered the event.

三月梨花 2024-09-23 04:38:32

显然 TreeViewItem.Loaded 事件是一个直接事件。所以没有办法完成我正在尝试的事情。

Apparently the TreeViewItem.Loaded event is a direct event. So there is no way to accomplish what I was trying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文