0

我见过几个类似问题的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 

...

所以我缺少什么这里?帮助赞赏,谢谢!

回答

2

您的HierarchicalDataTemplates与树的同一级别,因为for循环将它们放入同一个集合中。本质上,您的收藏与您在XAML中的绑定不匹配。

我相信要得到这个工作,你需要单独的集合为你想要的每个级别。所以你需要一个非洲大陆的收藏品和一个国家的收藏品。

然后你需要更新你的资源,这一点:

 <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 Path=Continents}"> 
         <TextBlock Text="{Binding Path=ContinentCode}" /> 
        </HierarchicalDataTemplate> 
       <HierarchicalDataTemplate DataType="{x:Type local:CountryViewModel}" ItemsSource="{Binding Path=Countries}"> 
         <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> 

这应该给你想要的东西。我已经使用相同的模型来定义树视图的消息结构,我们的代码之间的唯一区别是我有自己的集合中的每个级别,并显式绑定到该集合。

+0

嗨...你说的对,那是问题...非常感谢! :) – dennis

+0

我遇到过类似的问题。我使用的是基类集合的继承,并认为它足够聪明,可以使用类型集合和层次结构。只要我把它们分解成单独的集合,它就会更好。 – Josh