September 26th, 2010 03:29 PM
#1
TreeView, ContextMenu, how do I get the SelectedItem's data Node/object?
Hi all,
I've implemented a tree and have bound it to my treeview. the binding is fine, but id like to add "add child" "remove node" functionality in a context menu for a selected item. I can not for the life of me figure out how to do this. My code is below. please help me. please point me in the right direction.
<TreeView Name="treeView" Margin="10,10,14,10" BorderThickness="2" ItemsSource="{Binding Nodes}">
<TreeView.BorderBrush>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF332187" Offset="0" />
<GradientStop Color="Transparent" Offset="1" />
</LinearGradientBrush>
</TreeView.BorderBrush>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<TextBlock Text="{Binding title}" PreviewMouseRightButtonUp="createContextMenu"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
my tree implementation:
public class Node
{
private ObservableCollection<Node> _nodes;
public string title { get; set; }
public string content { get; set; }
public Node(string _title)
{
title = _title;
_nodes = new ObservableCollection<Node>();
}
public ObservableCollection<Node> Nodes
{
get
{
if (_nodes == null)
_nodes = new ObservableCollection<Node>();
return _nodes;
}
set
{
_nodes = value;
}
}
}
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks