如何在WPF中创建一个主视图不可选择且子项可选的树视图?

时间:2017-06-21 06:24:27

标签: c# wpf

我有这段代码:

var items = new TreeViewItem();
items.Header = "all Items";
items.Items.Add(new TreeViewItem() { Header = "a" });
items.Items.Add(new TreeViewItem() { Header = "b" });
items.Items.Add(new TreeViewItem() { Header = "c" });

但"所有项目"标题是可选择的。

如何阻止标题可选?

谢谢,

1 个答案:

答案 0 :(得分:0)

这样的东西?

 <TreeView>
        <TreeViewItem Header="all items" Focusable="False" >
            <TreeViewItem  Header="a"/>
            <TreeViewItem  Header="b"/>
            <TreeViewItem  Header="c"/>
        </TreeViewItem>
  </TreeView>

或者如果你想要代码:

        TreeView t = new TreeView();
        TreeViewItem root = new TreeViewItem();
        root.Header = "all items";
        root.Focusable = false;
        TreeViewItem t1 = new TreeViewItem();
        root.Header = "a";
        TreeViewItem t2 = new TreeViewItem();
        root.Header = "b";
        TreeViewItem t3 = new TreeViewItem();
        root.Header = "c";
        root.Items.Add(t1);
        root.Items.Add(t2);
        root.Items.Add(t3);
        t.Items.Add(root);