WPF TreeView HierarchicalDataTemplate - 绑定到具有多个子集合的对象 ->重装上阵
我看过几个有类似问题的帖子,并且我已经尝试全部关注它们。现在我终于在屏幕上看到了所有数据,但不是我想要的方式。但让我们开始吧。
我有一个 ViewModel,它提供如下数据:
/// <summary>
/// Returns a collection of all the ContinentListViewModel objects
/// </summary>
public static IList<Object> Items
{
get
{
IList<object> childNodes = new List<object>();
foreach (ContinentViewModel continent in _instance)
{
childNodes.Add(continent);
foreach (CountryViewModel country in continent.CountrytList)
{
childNodes.Add(country);
foreach (BusinessunitViewModel bu in country.BusinessunitList)
{
childNodes.Add(bu);
}
}
}
return childNodes;
}
}
这是我的 xaml-TreeView-code:
<TreeView Name="tvwBuList" ItemsSource="{Binding BusinessunitList.Items}" HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="230" MinWidth="265" MaxHeight="230" MaxWidth="265">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ContinentViewModel}" ItemsSource="{Binding}">
<TextBlock Text="{Binding Path=ContinentCode}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:CountryViewModel}" ItemsSource="{Binding}">
<TextBlock Text="{Binding Path=CountryCode}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:BusinessunitViewModel}">
<RadioButton GroupName="BUGroup" Content="{Binding Path=BuCode}" />
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<!--Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/-->
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Foreground" Value="Yellow"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
这就是输出的样子:
http://imgh.us/bu_treeview.jpg
我想要的是一个像这样的完整结构:
+ AP
|--+ AU
O-- 164
|--+ CN
O-- 258
O-- 277
...
那么我在这里缺少什么?帮助不胜感激,谢谢!
I've seen several posts with similar problems and I've already tried to follow them all. Now finally I get all data on screen, but not the way I want it to be. But let's get started.
I have a ViewModel which provides the data like this:
/// <summary>
/// Returns a collection of all the ContinentListViewModel objects
/// </summary>
public static IList<Object> Items
{
get
{
IList<object> childNodes = new List<object>();
foreach (ContinentViewModel continent in _instance)
{
childNodes.Add(continent);
foreach (CountryViewModel country in continent.CountrytList)
{
childNodes.Add(country);
foreach (BusinessunitViewModel bu in country.BusinessunitList)
{
childNodes.Add(bu);
}
}
}
return childNodes;
}
}
This is my xaml-TreeView-code:
<TreeView Name="tvwBuList" ItemsSource="{Binding BusinessunitList.Items}" HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="230" MinWidth="265" MaxHeight="230" MaxWidth="265">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ContinentViewModel}" ItemsSource="{Binding}">
<TextBlock Text="{Binding Path=ContinentCode}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:CountryViewModel}" ItemsSource="{Binding}">
<TextBlock Text="{Binding Path=CountryCode}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:BusinessunitViewModel}">
<RadioButton GroupName="BUGroup" Content="{Binding Path=BuCode}" />
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<!--Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/-->
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Foreground" Value="Yellow"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
And this is what the output looks like:
http://imgh.us/bu_treeview.jpg
What I want to have is a complete structure like this:
+ AP
|--+ AU
O-- 164
|--+ CN
O-- 258
O-- 277
...
So what am I missing here? Help appreciated, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 HierarchicalDataTemplates 位于树的同一级别,因为您的 for 循环将它们放入同一集合中。本质上,您的集合与 XAML 中的绑定不匹配。
我相信要使其发挥作用,您需要为您想要的每个级别单独收集。因此,您需要一个针对非洲大陆的集合,另一个针对各个国家/地区的集合。
然后你需要将你的资源更新为:
这应该会给你你想要的。我使用相同的模型通过树视图定义消息结构,我们的代码之间的唯一区别是我将每个级别都放在自己的集合中,并显式绑定到该集合。
Your HierarchicalDataTemplates are at the same level of the tree because your for loops are putting them into the same collection. Essentially your collection doesn't match your bindings in XAML.
I believe to get this to work you would need separate collections for each level you want. So you would need one collection for the continent and a second for the countries.
Then you would need to update your resources to this:
That should give you what you want. I've used the same model for defining a message structure with the treeview and the only difference between our code is I have each level in its own collection and bind explicitly to that collection.