WPF C# TreeView 获取所选项目的文本
Posted
技术标签:
【中文标题】WPF C# TreeView 获取所选项目的文本【英文标题】:WPF C# TreeView get text of selected item 【发布时间】:2021-12-22 04:04:15 【问题描述】:我有以下 TreeView:
<TreeView x:Name="TreeView"
MouseDoubleClick="TreeView_MouseDoubleClick"
Width="350">
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="CogOutline" Margin="0 0 5 0"/>
<TextBlock Text="Configuration" FontWeight="Bold"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="TruckDeliveryOutline" Margin="0 0 5 0"/>
<TextBlock Text="Items"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ArrowUpBoldBoxOutline" Margin="0 0 5 0"/>
<TextBlock Text="Configuration"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="FileDocumentOutline" Margin="0 0 5 0"/>
<TextBlock Text="List"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ClipboardMultipleOutline" Margin="0 0 5 0"/>
<TextBlock Text="Management"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ClipboardListOutline" Margin="0 0 5 0"/>
<TextBlock Text="Management"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ClipboardPlusOutline" Margin="0 0 5 0"/>
<TextBlock Text="Sundry Items"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="RelationOneToMany" Margin="0 0 5 0"/>
<TextBlock Text="Sundry Addition"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ClipboardPlusOutline" Margin="0 0 5 0"/>
<TextBlock Text="Sundry Addition"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="RelationOneToMany" Margin="0 0 5 0"/>
<TextBlock Text="Addition Relations"/>
</StackPanel>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="FormatListBulleted" Margin="0 0 5 0"/>
<TextBlock Text="Tasks" FontWeight="Bold"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="TruckDeliveryOutline" Margin="0 0 5 0"/>
<TextBlock Text="Items"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ChartFinance" Margin="0 0 5 0"/>
<TextBlock Text="(Converted)"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ChartFinance" Margin="0 0 5 0"/>
<TextBlock Text="(Each)"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="CurrencyGbp" Margin="0 0 5 0"/>
<TextBlock Text="Material"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="CurrencyGbp" Margin="0 0 5 0"/>
<TextBlock Text="Product"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Check" Margin="0 0 5 0"/>
<TextBlock Text="Validation"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Update" Margin="0 0 5 0"/>
<TextBlock Text="Update All"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Salesforce" Margin="0 0 5 0"/>
<TextBlock Text="Bundle"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Calculator" Margin="0 0 5 0"/>
<TextBlock Text="Calculator"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="HelpCircleOutline" Margin="0 0 5 0"/>
<TextBlock Text="Help" FontWeight="Bold"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="BookOpenOutline" Margin="0 0 5 0"/>
<TextBlock Text="Guide"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Link" Margin="0 0 5 0"/>
<TextBlock Text="Link"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
它将用作菜单表单中的导航,但我正在努力创建一个事件,该事件将在单击某些内容时触发,然后返回显示在树视图中的项目上的文本值。
我尝试过以下文章:
Getting the value of the Selected Child Nodes on click Event in WPF TreeView
How to add WPF treeView Node Click event to get the node value
同时使用这两种解决方案,对我不起作用,因为我无法在其中一种情况下访问 item.header
,而在其他情况下,它似乎无法正确创建 TreeViewItem item = sender as TreeViewItem
。
我是否将硬编码的TreeView
过度复杂化到无法创建此类事件以返回所单击项目的文本值的程度?
【问题讨论】:
【参考方案1】:您可以为每个 TreeViewItem 指定 Tag 属性。 在 MouseClick 事件处理程序中->您将能够将发件人转换为 TreeViewItem.. 和 那么你就可以获取它的 Tag 属性了
<TreeViewItem Tag="Link" MouseLeftButtonDown="ClickItem_Handler">
...
</TreeViewItem>
在表单的代码隐藏中
protected void ClickItem_Handler(object sender, MouseEventArgs args)
if (sender is TreeViewItem item)
var header = item.Tag as string;
【讨论】:
完美运行!我用MouseDoubleClick
事件代替了OnClick
。以上是关于WPF C# TreeView 获取所选项目的文本的主要内容,如果未能解决你的问题,请参考以下文章