class Category
{
public string Name;
public string Message;
public ObservableCollection<Category> SubCategories;
}
在那里将有说5个类别,每个类别包含0(无)之间小类说3
我知道如何结合非分层数据到WPF TreeView,但不能算出分层数据值。
class Category
{
public string Name;
public string Message;
public ObservableCollection<Category> SubCategories;
}
在那里将有说5个类别,每个类别包含0(无)之间小类说3
我知道如何结合非分层数据到WPF TreeView,但不能算出分层数据值。
下面是一个例子.....
<!-- 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它可能是更有益的给你读过那篇文章,我在想。
首先,您需要将所有这些字段转换为属性 - WPF数据绑定无法绑定到字段。 (然后,Muad'Dib的答案应该工作。)
谢谢,是的,这只是一个快速示例,不想完全编写它作为示例,但在实际的代码中,它们是属性。 – 2010-11-10 23:34:05
我知道这个问题很久以前被问过......但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>
我发现两者结合为我很好地工作。
谢谢你,我现在就试试这个。 – 2010-11-10 23:26:40
其实对不起,我无法在xaml中得到这个工作,只能在代码中工作。所以我用的是这样的:对于“{Binding Path = Animals}”,我使用了{Binding Path = Categories}。对于{Binding Path = Category}使用相同的,因为我认为这意味着输入类型的名称,对于“{Binding Path = Name}”,使用相同的,认为这是要显示的成员。 – 2010-11-10 23:55:32