[英]Get selected Text from WPF Tree View
I am displaying a tree view in WPF as follows. 我在WPF中显示树状视图,如下所示。
<TreeView Height="332" Background="Transparent" BorderBrush="White" HorizontalAlignment="Left" Margin="0,46,0,0" Name="treeView1" VerticalAlignment="Top" Width="182" MouseDoubleClick="treeView1_MouseDoubleClick" SelectedItemChanged="treeView1_SelectedItemChanged">
<TreeViewItem >
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/Gway.png" Height="24" Width="24"></Image>
<Label Content="Gway"></Label>
</StackPanel>
</TreeViewItem.Header>
<!-- Child Item -->
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/Scan.png" Height="24" Width="24"></Image>
<Label Content="Scan"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/Configuration.png" Height="24" Width="24"></Image>
<Label Content="Config"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem >
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/Fiware_1.png" Height="24" Width="24"></Image>
<Label Content="Fi ware"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem >
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/data.png" Height="24" Width="24"></Image>
<Label Content="Request"></Label>
</StackPanel>
</TreeViewItem.Header>
<!-- Child Item -->
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/assign.png" Height="24" Width="24"></Image>
<Label Content="Assign"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/update.png" Height="24" Width="24"></Image>
<Label Content="Update"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/UnAssign.png" Height="24" Width="24"></Image>
<Label Content="Unassign"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Logos/Delete.png" Height="24" Width="24"></Image>
<Label Content="Delete"></Label>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem>
</TreeView>
And trying to handle the double click event on a node. 并尝试处理节点上的双击事件。 But not able to get the selected text/value(Here it is Gway,Scan etc..) from the event arguments/sender.
但是无法从事件参数/发送方获取选定的文本/值(此处为Gway,Scan等。)。 The following is the event handler code.
以下是事件处理程序代码。
private void treeView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
object s = e.Source;
TreeView obj = (TreeView) s;
TreeViewItem item2 =(TreeViewItem)obj.SelectedItem;// item2 doesnt have any selctedText/ value property
}
please help me out. 请帮帮我。
Since it is the tree view item it has a tree beneath so it is not just a simple text, so in order to get what is there you can refer to the following and use any approach which suits your need 由于它是树视图项目,因此它下面有一棵树,因此它不仅仅是一个简单的文本,因此,要获取其中的内容,可以参考以下内容并使用适合您需要的任何方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.