0

我的 WPF 应用程序中有一个Treeview。我需要复制树视图中的元素。

private void copyCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    // Set text to clip board 
    TreeView tvi = (TreeView)sender;
    Clipboard.SetText(tvi.SelectedValue.ToString());
}

但是这里tvi.SelectedValue不会返回我在树视图中选择的文本。如何获取我在treeview.

更新:

<TreeView Grid.Column="0" Grid.Row="0"  
          HorizontalAlignment="Stretch" Name="treeView1" 
          SelectedItemChanged="treeView1_SelectedItemChanged">
    <TreeView.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Copy" 
                        Executed="copyCmdBinding_Executed" 
                        CanExecute="copyCmdBinding_CanExecute"/>
    </TreeView.CommandBindings>
    //Tree elements
</TreeView>
4

2 回答 2

1

试试这个:

((TreeViewItem)tvi.SelectedItem).Header.ToString())

一般来说,这段代码可能很有用:

((T1)tvi.SelectedItem).P.ToString()) 
// T1: type of property that is binded, 
// P: proper method or property that hold the string content
于 2013-01-23T11:47:17.820 回答
0

树中的项目可以是任何类型,并返回由的SelectedValue指定的属性的值。因此,请确保您已在 Treeview 中设置!SelectedValuePathSelectedItemSelectedValuePath

于 2013-01-23T11:51:29.097 回答