获取TreeView逻辑元素的TreeViewItem

时间:2014-07-21 07:14:44

标签: c# wpf treeview treeviewitem visualtreehelper

我的树视图看起来像这样

<TreeView x:Name="ArticlesTreeView" Grid.Column="0" AllowDrop="True">
            <TreeView.Resources>
                <HierarchicalDataTemplate   DataType="{x:Type structure:NewsPaperDocument}" ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}" Tag="{Binding Object}" FontWeight="Bold" />
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperPage}" ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#00a300" />
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperTitle}" ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#da532c" />
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type structure:NewsPaperBlock}">
                    <TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#2b5797" />
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>

ArticlesTreeView.SelectedItem存储类NewsPaperDocument, NewsPaperPage, etc的实例中。如何将TreeViewItemSelectedItem相关联?我尝试使用VisualTreeHelper.GetParent(elem);,但SelectedItem没有DependencyObject

类型

UPD1 添加示例问题的简单示例。 item中的ArticlesTreeView_SelectedItemChanged始终null

XAML

<Window x:Class="TestTree.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:testTree="clr-namespace:TestTree"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TreeView x:Name="ArticlesTreeView" Grid.Column="0" AllowDrop="True" SelectedItemChanged="ArticlesTreeView_SelectedItemChanged">
        <TreeView.Resources>
            <HierarchicalDataTemplate   DataType="{x:Type testTree:A}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type testTree:B}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" Foreground="#00a300" />
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type testTree:C}">
                <TextBlock Text="{Binding Name}" Foreground="#2b5797" />
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

CS

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TestTree
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var a = new A
            {
                Name = "a",
                Children = new List<B>
                {
                    new B
                    {
                        Name = "b1",
                        Children = new List<C>
                        {
                            new C{Name = "c1"},
                            new C{Name = "c2"},
                            new C{Name = "c3"}
                        },
                    },
                    new B
                    {
                        Name = "b2",
                        Children = new List<C>
                        {
                            new C{Name = "c1"},
                            new C{Name = "c2"},
                            new C{Name = "c3"}
                        },
                    },
                    new B
                    {
                        Name = "b3",
                        Children = new List<C>
                        {
                            new C{Name = "c1"},
                            new C{Name = "c2"},
                            new C{Name = "c3"}
                        },
                    }
                }
            };
            ArticlesTreeView.ItemsSource = new List<A> { a };
        }

        private void ArticlesTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            ItemContainerGenerator gen = ArticlesTreeView.ItemContainerGenerator;
            var item = gen.ContainerFromItem(ArticlesTreeView.SelectedItem);
        }
    }

    internal class A
    {
        public string Name { set; get; }

        public List<B> Children { set; get; }
    }

    internal class B
    {
        public string Name { set; get; }

        public List<C> Children { set; get; }
    }

    internal class C
    {
        public string Name { set; get; }
    }
}

1 个答案:

答案 0 :(得分:1)

如果我找到你,你想从TreeViewItem

中检索SelectedItem

因此您可以使用ItemContainerGenerator

        ItemContainerGenerator gen = ArticlesTreeView.ItemContainerGenerator;
        TreeViewItem item = gen.ContainerFromItem(ArticlesTreeView.SelectedItem) as TreeViewItem;

从嵌套项目中获取TreeViewItem

由于每个TreeViewItem都有自己的ItemContainerGenerator,所以为了从嵌套项中找到容器,我们需要递归树的深度

    private void ArticlesTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        var item = ContainerFromItem(ArticlesTreeView.ItemContainerGenerator, ArticlesTreeView.SelectedItem);
    }

    private static TreeViewItem ContainerFromItem(ItemContainerGenerator containerGenerator, object item)
    {
        TreeViewItem container = (TreeViewItem)containerGenerator.ContainerFromItem(item);
        if (container != null)
            return container;

        foreach (object childItem in containerGenerator.Items)
        {
            TreeViewItem parent = containerGenerator.ContainerFromItem(childItem) as TreeViewItem;
            if (parent == null)
                continue;

            container = parent.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
            if (container != null)
                return container;

            container = ContainerFromItem(parent.ItemContainerGenerator, item);
            if (container != null)
                return container;
        }
        return null;
    }