0

我使用 WPF/MVVM 来玩弄树视图控件

<TreeView HorizontalAlignment="Left" 
                  Height="319" 
                  VerticalAlignment="Top" 
                  Width="517"
                  ItemsSource="{Binding Tree}"
                  >
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">
                        <Button Width="100" Height="20" IsEnabled="{Binding IsEnabled}" Content="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

这是我的节点类视图模型

public partial class MainWindow : Window
    {
        class TreeNode
        {
            public string Name { get; set; }
            public bool IsEnabled { get; set; }
            public List<TreeNode> Children { get; set; }
            public TreeNode()
            {
                Children = new List<TreeNode>();
                IsEnabled = true;
            }
        }

        class ViewModel
        {
            public List<TreeNode> Tree { get; private set; }
            public ViewModel()
            {
                Tree = new List<TreeNode>();
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            var viewModel = new ViewModel
                {
                    Tree =
                        {
                            new TreeNode
                                {
                                    Name = "Root 1",
                                    IsEnabled = false,
                                    Children = {
                                        new TreeNode { Name = "Child 1" },
                                        new TreeNode { Name = "Child 2" },
                                        new TreeNode { Name = "Child 3", 
                                            Children =
                                                {
                                                    new TreeNode { Name = "Child 3-1" },
                                                    new TreeNode { Name = "Child 3-2" },
                                                    new TreeNode { Name = "Child 3-3" }, 
                                                }
                                        },
                                    }
                                }
                        }
                };

            DataContext = viewModel;
        }
    }

如您所见,我将属性“IsEnabled”绑定到按钮,这一切都很好,但我实际上想将“IsEnabled”属性绑定到实际的根节点元素,而不是节点内的对象。

我需要做什么来根据这个属性值禁用整个根节点?

4

1 回答 1

0

为此,您实际上需要影响ItemContainerStyle. 这会设置为树的每个节点生成TreeView的 ItemContainer ( ) 的样式。TreeViewItem

<TreeView HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517" ItemsSource="{Binding Tree}">
    <TreeView.ItemTemplate>
        <!-- Leave this the same, except for the IsEnabled binding on the button -->
    </TreeView.ItemTemplate>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>
于 2013-04-23T13:17:24.313 回答