2013-07-05 69 views
2

我有一個ItemsControl,它綁定到一個集合,我指定一個HierarchicalDataTemplate來渲染項目。我也有一個模板選擇器,因爲單個項目的渲染要謹慎。出於某種原因,這是行不通的。與ItemsControl的層級數據模板綁定不起作用 - WPF

下面是代碼的摘錄,你能幫忙嗎?我正在尋找從以下層次結構父項 - >子集合 - > SubChildCollection呈現項目(如下面的代碼)。

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication2="clr-namespace:WpfApplication2" Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 

     <DataTemplate x:Key="EntityItemTemplate" DataType="{x:Type WpfApplication2:EntityItem}"> 
      <TextBlock Text="{Binding Name}"/> 
     </DataTemplate> 
     <WpfApplication2:TemplateSelector x:Key="ts" EntityItemTemplate="{StaticResource EntityItemTemplate}"/>   
     <HierarchicalDataTemplate x:Key="hdt" DataType="{x:Type WpfApplication2:EntityGroup}" ItemsSource="{Binding Path=EntityItems}" ItemTemplateSelector="{StaticResource ts}"/> 
    </Window.Resources> 

    <Grid> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <ItemsControl Grid.Row="0" ItemsSource="{Binding Path=Entity.EntityGroups}" ItemTemplate="{StaticResource hdt}"></ItemsControl> 
    </Grid> 
</Window> 

public partial class MainWindow : Window 
    { 
     ViewModel vm = new ViewModel(); 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = vm; 
      vm.Entity = new Entity() 
          { 
           Name = "abc", 
           EntityGroups = new ObservableCollection<EntityGroup>() 
                { 
                 new EntityGroup() 
                  { 
                   EntityItems = new ObservableCollection<EntityItem>() 
                       { 
                        new EntityItem() {Name = "Entity1"}, 
                        new EntityItem() {Name = "Entity2"} 
                       } 
                  } 
                } 
          }; 
     } 

    } 

    public class TemplateSelector:DataTemplateSelector 
    { 
     public DataTemplate EntityItemTemplate { get; set; } 

     public override DataTemplate SelectTemplate(object item, DependencyObject container) 
     { 
      if (item == null || !(item is EntityItem)) 
       return base.SelectTemplate(item, container); 

      return EntityItemTemplate; 
     } 
    } 

    public class ViewModel:NotificationObject 
    { 
     private Entity _entity; 
     public Entity Entity 
     { 
      get { return _entity; } 
      set 
      { 
       _entity = value; 
       RaisePropertyChanged(() => Entity); 
      } 
     } 
    } 

    public class Entity:NotificationObject 
    { 
     private string _name; 
     public string Name 
     { 
      get { return _name; } 
      set 
      { 
       _name = value; 
       RaisePropertyChanged(() => Name); 
      } 
     } 

     private ObservableCollection<EntityGroup> _entityGroups; 
     public ObservableCollection<EntityGroup> EntityGroups 
     { 
      get { return _entityGroups; } 
      set 
      { 
       _entityGroups = value; 
       RaisePropertyChanged(() => EntityGroups); 
      } 
     } 
    } 

    public class EntityGroup:NotificationObject 
    { 
     public string Name { get; set; } 

     private ObservableCollection<EntityItem> _entityItems; 
     public ObservableCollection<EntityItem> EntityItems 
     { 
      get { return _entityItems; } 
      set 
      { 
       _entityItems = value; 
       RaisePropertyChanged(() => EntityItems);  
      } 
     } 
    } 

    public class EntityItem:NotificationObject 
    { 
     private string _name; 
     public string Name 
     { 
      get { return _name; } 
      set 
      { 
       _name = value; 
       RaisePropertyChanged(() => Name); 
      } 
     } 
    } 

回答

3

因爲你在ItemsControl使用它,你不應該使用HierarchicalDataTemplate。如MSDN狀態,一個HierarchicalDataTemplate

表示支持HeaderedItemsControl一個DataTemplate,如 的TreeViewItem或菜單項。

ItemsControl將其數據顯示在ContentPresenter內。 A TreeView將生成TreeViewItems,並且Menu將生成MenuItems

如果你想使用DataTemplateSelectorItemsControl顯示的項目不同的模板,只需將它直接爲ItemTemplateSelector

<ItemsControl ItemsSource="{Binding Path=Entity.EntityGroups}" 
       ItemTemplateSelector="{StaticResource ts}" /> 

如果你希望你的數據的分層顯示,使用TreeView

<TreeView ItemsSource="{Binding Path=Entity.EntityGroups}" 
      ItemTemplate="{StaticResource hdt}" /> 
相關問題