My tree view seems like this
<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>
In ArticlesTreeView.SelectedItem
stores instance of classes NewsPaperDocument, NewsPaperPage, etc
. How can I get TreeViewItem
associated with SelectedItem
? I try to use VisualTreeHelper.GetParent(elem);
, but SelectedItem
doesn't has type DependencyObject
UPD1 Add simple sample, that demonstrate problem. item
in ArticlesTreeView_SelectedItemChanged
always 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; }
}
}