>I have a WPF TreeView bound to a hierarchical set of data, like this
http://www.syncfusion.com/FAQ/WPF/wpf_c95c.aspx.
>
>It works fine, except that I need to get back the selected item from the tree so I can look at it's parent. My TreeView is a user control:
>
>
><UserControl.Resources>
>
> <!-- TreeViewItem Style -->
> <Style TargetType="TreeViewItem">
> <EventSetter Event="Selected" Handler="TreeItemSelected"/>
> </Style>
>
></UserControl.Resources>
>
><Grid>
>
> <TreeView
> Name="tvwRules"
> HorizontalAlignment="Stretch"
> VerticalAlignment="Stretch"
> ItemsSource="{Binding Path=Manager.Root.Groups}">
>
> <TreeView.Resources>
> <SolidColorBrush Color="LightYellow" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
> <SolidColorBrush Color="Black" x:Key="{x:Static SystemColors.HighlightTextBrushKey}"/>
> </TreeView.Resources>
>
> </TreeView>
>
></Grid>
>
>
>In the code behind, the TreeViewItem Selected event at the top of the XAML above is handled like this:
>
>
>public TreeViewItem SelectedItem { get; private set; }
>private void TreeItemSelected(object sender, RoutedEventArgs e)
>{
> SelectedItem = (TreeViewItem)sender;
> object Parent = SelectedItem.Parent // Always null
> e.Handled = true;
>}
>
>
>When this is run, SelectedItem is indeed the TreeViewItem, but it's Parent is always NULL.
>
>I created a simple example window with a tree that contains 2 nodes. Clicking the child node triggers the event above, and
>SelectedItem.Parent is a TreeViewItem.
>
>But in my data bound application SelectedItem.Parent is always null.
>
>Anyone see what's wrong?
Nothing is wrong that IS the expected behavior. See this:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/332833fc-11c9-44d7-8d09-ab081ac0f28aBTW the WPF TreeVeiw is radically different from any other TreeView I've ever worked with. Are you using a HierarchicalDataTemplate? If not check them out they are slick. And... what are you trying to accomplish?
More often than not I barely touch the TreeView, I do most of the work in the data and in the bindings. I mostly just let the TreeView handle itself.