与ItemsControl绑定的分层数据模板不起作用 - WPF

时间:2013-07-05 16:18:00

标签: wpf itemscontrol hierarchicaldatatemplate

我有一个ItemsControl,它绑定到一个集合,我指定一个HierarchicalDataTemplate来呈现这些项目。我也有一个模板选择器,因为单个项目的渲染会变得谨慎。出于某种原因,这不起作用。

以下是代码的摘录,您能帮忙吗?我想要渲染以下层次结构中的项目Parent-> Child Collection-> 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);   
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

因为您在ItemsControl中使用它,所以不应使用HierarchicalDataTemplate。正如MSDN所述,HierarchicalDataTemplate

  

表示支持HeaderedItemsControl的DataTemplate,例如   TreeViewItem或MenuItem。

ItemsControlContentPresenter内显示其数据。 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}" />