【问题标题】:WPF Programmatically create treeview itemtemplate/columnsWPF 以编程方式创建树视图项模板/列
【发布时间】:2011-05-26 15:05:23
【问题描述】:

我有一个读取数据库表并将其放入树视图的应用程序。树视图的当前 ItemTemplate 如下所示:

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="35" />
                <ColumnDefinition Width="35" />
                <ColumnDefinition Width="35" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding OrganDisplayName}" />
            <TextBox Grid.Column="1" IsEnabled="True" />
            <TextBox Grid.Column="2" IsEnabled="True" />
            <TextBox Grid.Column="3" IsEnabled="True" />
        </Grid>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

但是,将来可能需要添加更多列(由表中不同值的数量确定),因此我尝试动态创建它。我该怎么做呢?

【问题讨论】:

  • 用自定义控件替换您的网格,该控件将您用于ItemSource 的任何对象用于动态创建网格。据我所知,您无法在 XAML 中以声明方式执行您要查找的内容。
  • 谢谢,我不认为它可以在 XAML 中完成,这就是我有点困惑的原因。如何创建自定义控件?既然这将创建网格(而不是 XAML 代码),那么相应的 XAML 代码会是什么样子?

标签: c# wpf treeview itemtemplate


【解决方案1】:

这样的事情可能是可能的:

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
        <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding OrganDisplayName}" />

                <!-- If the fields to bind to can be exposed via a collection:
                <ItemsControl ItemsSource="{Binding Fields}"> -->
                <ItemsControl ItemsSource="{Binding, Converter={StaticResource SomeCleverConverter}}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Value}" Width="35" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

            </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

这取决于 TreeViewItem(SubOrganLocation)的 DataContext 是否可以公开字段集合,或者至少用于通过转换器派生它们。最简单也可能最简单的方法是公开一组字段,这样您就可以执行{Binding Fields}

【讨论】:

    【解决方案2】:

    正如 FlyingStreudel 所说,在这种情况下使用自定义控件是一种很好的方法。 XAML 看起来像:

            <!-- entity 1 -->
            <HierarchicalDataTemplate DataType="{x:Type local:Entity1}" ItemsSource="{Binding Items, Mode=OneWay}">
                <Grid>
                    <local:Entity1Control/>
                </Grid>
            </HierarchicalDataTemplate>
    
            <!-- entity 2 (leaf) -->
            <DataTemplate DataType="{x:Type local:Entity2}">
                <Grid>
                    <local:Entity2Control />
                </Grid>
            </DataTemplate>
    

    您不必使用自定义控件,您可以将您的特定列放在每个模板中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2019-10-10
      • 2017-05-01
      相关资源
      最近更新 更多