我见过几个类似问题的posit,我已经试着去关注它们。现在我终于获得了屏幕上的所有数据,但不是我想要的样子。但是,让我们开始吧...WPF TreeView HierarchicalDataTemplate - 绑定到具有多个子集合的对象 - > Reloaded
我有一个视图模型提供这样的数据:
/// <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控件代码:
<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
...
所以我缺少什么这里?帮助赞赏,谢谢!
嗨...你说的对,那是问题...非常感谢! :) – dennis
我遇到过类似的问题。我使用的是基类集合的继承,并认为它足够聪明,可以使用类型集合和层次结构。只要我把它们分解成单独的集合,它就会更好。 – Josh