【发布时间】:2010-11-30 22:27:25
【问题描述】:
那么,以下在 WPF 中很简单,但在 Silverlight 中您将如何做到这一点?
请注意,这里的技巧是在同一级别上显示组和条目。 此外,您不知道条目嵌套的深度,它们可能位于第一级或第 n 级。 这在 Silverlight 中很难,因为您在 H.DataTemplate(或任何 DataTemplate)中缺少 DataType="{x:Type local:Group}" 属性。构建我自己的自定义 DataTempalteSelector 也不起作用,因为 Hierarchial ItemsSource 丢失了。 (这只是给了我一个新的想法,我将很快调查)
例子:
Group1
--Entry
--Entry
Group2
--Group4
----Group1
------Entry
------Entry
----Entry
----Entry
--Entry
--Entry
Group3
--Entry
--Entry
你的课程:
public class Entry
{
public int Key { get; set; }
public string Name { get; set; }
}
public class Group
{
public int Key { get; set; }
public string Name { get; set; }
public IList<Group> SubGroups { get; set; }
public IList<Entry> Entries { get; set; }
}
你的xml:
<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource={Binding Items}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Entry}" >
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
【问题讨论】:
-
这个 ItemsSource="{Binding Items}" 真的可以在 WPF 中工作吗?这是什么“物品”?
-
好点,我自己没有测试,我只是参考了这篇文章:stackoverflow.com/questions/4150334/treeview-binding 给出了 sn-p 作为答案。
-
我认为 Items 应该只更改为 SubGroups(并且 TreeView 中的 Binding 应该是 obs.collection
) -
您可能应该将您提到解决方案的评论作为答案,并将其标记为答案。
标签: wpf silverlight treeview hierarchicaldatatemplate