如何在WPF中获取TreeView的单击节点的标题?

时间:2018-05-22 20:22:30

标签: c# wpf treeview treeviewitem

这是我的TreeView:

<TreeView x:Name="Tree">
   <TreeView.ItemContainerStyle>
      <Style TargetType="{x:Type TreeViewItem}">
         <EventSetter Event="MouseDoubleClick" Handler="TreeViewItem_MouseDoubleClick"/>
      </Style>
   </TreeView.ItemContainerStyle>
</TreeView>

无论点击哪个节点,发件人的Header属性都是树根的Header。

1 个答案:

答案 0 :(得分:0)

您可以从SourceMouseButtonEventArgs中获得

private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
   var sel = e.Source as TreeViewItem;
   if(sel != null)
   {
      String text = sel.Header as String;
   }
}