【问题标题】:WPF TreeView - Confusing data binding and style for child nodesWPF TreeView - 混淆子节点的数据绑定和样式
【发布时间】:2015-03-18 09:58:02
【问题描述】:

我对 TreeView 的 WPF 数据绑定和样式声明感到困惑,尤其是对于子节点。

我的 ViewModel 包含具有以下层次结构的对象:

- Component1
  - SubcomponentA
  - SubcomponentB
- Component2
  - SubcomponentX
  - SubcomponentY
  - SubcomponentZ

我想修改 XAML 文件,因此我不必在 .cs 文件中执行任何操作。

这段代码确实有效:

<TreeView Name="tvComponent" ItemsSource="{Binding BpModule.BpComponentPrototypes.Elements}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding BpSubcomponents.Elements}">
            <StackPanel Orientation="Horizontal">
                <CheckBox Name="cb_run"></CheckBox>
                <TextBlock Text="{Binding ShortName}" />
            </StackPanel>
        </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
 </TreeView>

但是,我想为根节点和子节点创建不同的样式。 我用几乎完全不同的 XAML 代码尝试了不同的方法。但主要问题是描述子节点绑定到其父节点的依赖关系,因此它们在运行时保持为空。

你能帮帮我吗?

谢谢。

【问题讨论】:

    标签: c# wpf xaml mvvm treeview


    【解决方案1】:

    在我看来,您可以使用隐式 DataTemplate 机制(查看 here 到 DataType 属性)。通过这种方式,您可以定义更多DataTemplates,每个Type“链接”到一个特定的Type

    <TreeView Name="tvComponent" ItemsSource="{Binding BpModule.BpComponentPrototypes.Elements}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:RootType}"ItemsSource="{Binding ...}">
                ...
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:ComponentType}" ItemsSource="{Binding BpSubcomponents.Elements}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Name="cb_run"></CheckBox>
                    <TextBlock Text="{Binding ShortName}" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type local:SubcomponentType}">
                ...
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>
    

    否则,您始终可以使用DataTemplateSelector 创建自己的逻辑,为您选择正确的模板。

    【讨论】:

    • 这是我已经尝试过的一种方法。 'Binding BpModule.BpComponentPrototypes.Elements' 已经定义了我的根元素。那么我必须向 RootType 中的 ItemSource Binding 添加什么?另外,第二个“HierarchicalDataTemplate”是否隐式绑定到根节点?
    • 如果您发布要在 TreeView 中表示的对象(视图模型)的代码可能会更好
    【解决方案2】:

    嗯,不知何故,我为根节点和子节点提供了不同的设计。这是 XAML 代码:

    <TreeView Name="tvSoftwareComponentPrototypes" ItemsSource="{Binding Path=BpModule.BpComponentPrototypes.Elements}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding BpSubcomponents.Elements}">
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox Name="cb_run"></CheckBox>
                                    <Image Source="/Resources/SubComponent.png" Margin="5,0,3,0" />
                                    <TextBlock Text="{Binding ShortName}" />
                                </StackPanel>
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Name="cb_swc" Checked="cb_swc_Checked" Unchecked="cb_swc_Unchecked"></CheckBox>
                                <Image Source="/Resources/Component.png" Margin="5,0,3,0" />
                                <TextBlock Text="{Binding ShortName}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>
    

    下一个问题是我无法在运行时访问复选框。 WPF 吓到我了:/

    编辑:

    等等..实际上这很容易通过将复选框更改为以下 XAML 代码:

     <CheckBox Name="cb_swc" IsChecked="{Binding Path=PropertyName, Mode=TwoWay}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多