Getting TreeViewItem for the selected item in a TreeView
I have a TreeView that is bound to a dataset which is having parent child relation. How i will get seleted TreeViewItem from the TreeView? Please help me. My code is below.
xaml:-
<TreeView Name="tvPersonal" Background="Transparent" ItemsSource="{Binding RootNodes}" SelectedItemChanged="tvPersonal_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding rsParentChild}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
public MainWindow()
开发者_Python百科 {
InitializeComponent();
DataSet ds = new BL.BLMenu().GetAllMenues(new BOModule { Name = Modules.Personnel });
ds.Tables[0].TableName = "Menu";
DataRelation relation = new DataRelation("rsParentChild",
ds.Tables["Menu"].Columns["MenuId"],
ds.Tables["Menu"].Columns["ParentId"]);
relation.Nested = true;
ds.Relations.Add(relation);
BOMenu mnu = new BOMenu();
BOMenu.RootNodes = ds.Tables["Menu"].DefaultView;
BOMenu.RootNodes.RowFilter = "ParentId IS NULL";
this.DataContext = this;
stbiDate.Content = DateTime.Now;
}
public DataView RootNodes
{
get { return BOMenu.RootNodes; }
}
BOMenu Class :-
public class BOMenu
{
public string MenuId
{
get;
set;
}
public string Name
{
get;
set;
}
public string ParentId
{
get;
set;
}
public int Priority
{
get;
set;
}
public static DataView RootNodes
{
get;
set;
}
}
While biju's answer works for flat hierarchies, I had to look for a solution for HierarchicalDataTemplates. This is the extension method that worked for me:
public static TreeViewItem ContainerFromItemRecursive(this ItemContainerGenerator root, object item)
{
var treeViewItem = root.ContainerFromItem(item) as TreeViewItem;
if (treeViewItem != null)
return treeViewItem;
foreach (var subItem in root.Items)
{
treeViewItem = root.ContainerFromItem(subItem) as TreeViewItem;
var search = treeViewItem?.ItemContainerGenerator.ContainerFromItemRecursive(item);
if (search != null)
return search;
}
return null;
}
You can use it with
TreeViewItem tvi = treeView
.ItemContainerGenerator
.ContainerFromItemRecursive(treeView.SelectedItem);
Try
TreeViewItem tvi = myTree.ItemContainerGenerator.ContainerFromItem(SelectedItem) as TreeViewItem;
or go through the below links.Hope this helps
Data binding to SelectedItem in a WPF Treeview
Get SelectedItem from TreeView?
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/36aca7f7-0b47-488b-8e16-840b86addfa3/
This works for trees whose data source is bound via a HierarchicalDataTemplate
Handle TreeViewItem.Selected
<TreeView Name="mTreeView" TreeViewItem.Selected="TreeViewItem_OnItemSelected" />
And set the TreeViewItem as the Tag.
private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
mTreeView.Tag = e.OriginalSource;
}
Which you can retrieve later
TreeViewItem tvi = mTreeView.Tag as TreeViewItem;
The best solution I've found involves a simple helper method and can be used in virtually any of the TreeView's events (i.e., SelectedItemChanged, MouseLeftButtonUp, etc.).
TreeViewItem Item = TreeViewHelper.VisualUpwardSearch(e.OriginalSource as DependencyObject);
I am using multiple hierarchy data templates and this is the only method that worked for me. Now, I'm able to create a new control based on TreeView and can handle all events involving the selected item internally.
public static TreeViewItem VisualUpwardSearch(DependencyObject source)
{
while (source != null && !(source is TreeViewItem)) source = System.Windows.Media.VisualTreeHelper.GetParent(source);
return source as TreeViewItem;
}
精彩评论