【问题标题】:How to data bind a hierarchical data to a WPF TreeView?如何将分层数据数据绑定到 WPF TreeView?
【发布时间】:2010-11-10 23:15:42
【问题描述】:

类型如下:

class Category
{
    public string Name;
    public string Message;

    public ObservableCollection<Category> SubCategories;
}

它将有 5 个类别,其中每个类别包含 0(无)到 3 个之间的子类别。

我知道如何将非分层数据绑定到 WPF TreeView,但无法确定分层数据值。

【问题讨论】:

    标签: c# .net wpf data-binding


    【解决方案1】:

    这是一个例子.....

    <!-- Create a TreeView, and have it source data from
           the AnimalCategories collection -->
      <TreeView ItemsSource="{x:Static local:Window1.AnimalCategories}">
     
        <!-- Specify the template that will display a node
             from AnimalCategories.  I.e., one each for “Amphibians”
             and “Spiders” in this sample.  It will get its nested
             items from the "Animals" property of each item -->
        <TreeView.ItemTemplate>
          <HierarchicalDataTemplate ItemsSource="{Binding Path=Animals}">
     
            <!-- Display the AnimalCategory by showing it's Category string -->
            <TextBlock FontWeight="Bold" Text="{Binding Path=Category}" />
     
            <!-- Specify the nested template for the individual Animal items
                 that are within the AnimalCategories.  E.g. “California Newt”, etc. -->
            <HierarchicalDataTemplate.ItemTemplate>
              <DataTemplate>
                <TextBlock Text="{Binding Path=Name}"/>
              </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
           
          </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
      </TreeView>
    

    这段代码来自here,我在想,阅读那篇文章可能对你更有帮助。

    【讨论】:

    • 其实很抱歉,我无法在 xaml 中使用它,只能在代码中使用。所以我使用的是:对于“{Binding Path=Animals}”,我使用了{Binding Path=Categories}。对于 {Binding Path=Category} 使用相同,因为我认为这意味着输入类型的名称,对于“{Binding Path=Name}”,使用相同,认为这是要显示的成员。
    【解决方案2】:

    我知道很久以前有人问过这个问题....但是 MSDN 上有一个非常好的 example 扩展了 Muad'Dib 的答案。

    他们的 XAML 如下所示:

        <StackPanel x:Name="LayoutRoot" Background="White">
            <StackPanel.Resources>
                <HierarchicalDataTemplate x:Key="ChildTemplate" >
                    <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" />
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate x:Key="NameTemplate" ItemsSource="{Binding Path=ChildTopics}" ItemTemplate="{StaticResource ChildTemplate}">
                    <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" />
                </HierarchicalDataTemplate>
            </StackPanel.Resources>
            <TreeView Width="400"  Height="300" ItemsSource="{Binding}" ItemTemplate="{StaticResource NameTemplate}" x:Name="myTreeView" />
        </StackPanel>
    

    我发现将两者结合起来非常适合我。

    【讨论】:

      【解决方案3】:

      首先,您需要将所有这些字段转换为属性 - WPF 数据绑定无法绑定到字段。 (然后,Muad'Dib 的答案应该有效。)

      【讨论】:

      • 谢谢,是的,这只是一个快速示例,不想将其完全编码为示例,但在实际代码中,它们是属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      相关资源
      最近更新 更多