Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Data Bound TreeView Question/Problem
Message
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01465307
Message ID:
01465425
Views:
35
Excellent!!

This now works:
TreeViewItem SelectedItem = RuleTree.SelectedItem;
ItemsControl SelectedItemParent = ItemsControl.ItemsControlFromItemContainer(SelectedItem);
object Data = SelectedItemParent.DataContext;
The parent's DataContect is what I was after.

Thanks!



>>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-ab081ac0f28a
>
>BTW 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.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Reply
Map
View

Click here to load this message in the networking platform