获取TreeView中所选项的TreeViewItem

时间:2011-03-07 10:15:02

标签: c# wpf

我有一个TreeView绑定到具有父子关系的数据集。我将如何从TreeView获取seleted TreeViewItem?请帮我。我的代码如下。

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()   

        {    
            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类: -

 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;
        }

    }

4 个答案:

答案 0 :(得分:12)

尝试

TreeViewItem tvi = myTree.ItemContainerGenerator.ContainerFromItem(SelectedItem) as TreeViewItem;

或通过以下链接。希望这有帮助

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/

答案 1 :(得分:7)

虽然biju的答案适用于平面层次结构,但我不得不寻找HierarchicalDataTemplates的解决方案。这是对我有用的扩展方法:

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;
}

您可以将其与

一起使用
TreeViewItem tvi = treeView
                       .ItemContainerGenerator
                       .ContainerFromItemRecursive(treeView.SelectedItem);

答案 2 :(得分:5)

这适用于通过HierarchicalDataTemplate

绑定数据源的树

处理TreeViewItem.Selected

<TreeView Name="mTreeView" TreeViewItem.Selected="TreeViewItem_OnItemSelected" />

并将TreeViewItem设置为Tag。

private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
    mTreeView.Tag = e.OriginalSource;
}

您可以稍后检索

TreeViewItem tvi = mTreeView.Tag as TreeViewItem;

答案 3 :(得分:1)

我发现的最佳解决方案涉及一个简单的辅助方法,几乎​​可以在任何TreeView事件中使用(即SelectedItemChanged,MouseLeftButtonUp等)。

TreeViewItem Item = TreeViewHelper.VisualUpwardSearch(e.OriginalSource as DependencyObject);

我正在使用多个层次结构数据模板,这是适用于我的唯一方法。现在,我可以基于TreeView创建一个新控件,并且可以在内部处理涉及所选项目的所有事件。

public static TreeViewItem VisualUpwardSearch(DependencyObject source)
{
    while (source != null && !(source is TreeViewItem)) source = System.Windows.Media.VisualTreeHelper.GetParent(source);
    return source as TreeViewItem;
}