从WPF树视图中获取选定的文本

时间:2014-06-12 05:13:31

标签: c# wpf treeview

我在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>

尝试处理节点上的双击事件。但是无法从事件参数/发送者获取所选文本/值(这里是Gway,Scan等..)。以下是事件处理程序代码。

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


    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

因为它是树视图项,所以它下面有一棵树,所以它不仅仅是一个简单的文本,所以为了得到那些,你可以参考以下内容并使用任何适合你需要的方法

  • TreeViewItem的标头属性,用于显示那里的标题(仅限设置)
  • 用于检索下面的集合的ItemsSource属性
  • DataContext,用于检索绑定到它的数据(将其强制转换为适当的类型)并检索所需的值。