HierachicalDataTemplate 和带有 ItemsControl 的 DataTemplate 之间的区别

Difference between HierachicalDataTemplate and DataTemplate with ItemsControl

我想知道 HierarchicalDataTemplate 与此 DataTemplate 中带有 Itemcontrol 的 DataTemplate 之间的真正区别。

下面是一些示例代码:

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:test="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type test:TeamViewModel}">
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>

        <!--<HierarchicalDataTemplate DataType="{x:Type test:TeamManagerViewModel}"
                                  ItemsSource="{Binding Path=Teams}"/>-->

        <DataTemplate DataType="{x:Type test:TeamManagerViewModel}">
            <ItemsControl ItemsSource="{Binding Path=Teams}"/>
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <ContentControl Content="{Binding}"/>
    </Grid>
</Window>

ViewModel.cs

public class ViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    /// <summary>
    /// Event used by binding
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notifies that a property has changed
    /// </summary>
    /// <param name="requestName">the property name</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

团队经理ViewModel.cs

public class TeamManagerViewModel : ViewModel
{
    private ObservableCollection<TeamViewModel> _teams;

    public TeamManagerViewModel(ObservableCollection<TeamViewModel> teams)
    {
        _teams = teams;
    }

    public ObservableCollection<TeamViewModel> Teams
    {
        get { return _teams; }
        set
        {
            _teams = value;
            OnPropertyChanged("Teams");
        }
    }
}

团队ViewModel.cs

public class TeamManagerViewModel : ViewModel
{
    private ObservableCollection<TeamViewModel> _teams;

    public TeamManagerViewModel(ObservableCollection<TeamViewModel> teams)
    {
        _teams = teams;
    }

    public ObservableCollection<TeamViewModel> Teams
    {
        get { return _teams; }
        set
        {
            _teams = value;
            OnPropertyChanged("Teams");
        }
    }
}

所以结果是 window 中出现的名称列表。但是,如果我用注释的 HierarchicalDataTemplate 替换 TeamManagerViewModel 的 DataTemplate,则不会显示任何内容。

对我来说这似乎很奇怪,因为 HierarchicalDataTemplate 的 ItemsSourceProperty 应该寻找 TeamViewModel DataTemplate 并使用它。 怎么样?

还有一个问题,我的样本和 MSDN 样本之间有什么区别(除了在 HierarchicalDataTemplate 中添加一个 TextBlock 之外,它们似乎和我做的一样)?

https://msdn.microsoft.com/fr-fr/library/system.windows.hierarchicaldatatemplate%28v=vs.110%29.aspx

另外,如果我的问题写得不好,请原谅,这是我第一次在SO上提问。

谢谢:)

HierarchicalDataTemplates 由 TreeView 使用。来自 documentation

A TreeView can populate its tree by binding to a data source and using HierarchicalDataTemplate objects.

ItemsControls 未明确显示分层数据的系统不会使用该模板。我想不起来我的名字 另一个 控件...可能是菜单?不确定。但是在 TreeView 中使用此模板类型意味着 您不必在每个不代表叶 的模板中添加子 TreeView。这样可以节省一些时间。

并非设计用于使用它们的控件肯定不会在您的资源中查找它们。


因为您似乎很在意它的实际使用位置,所以我打开了 JustDecompile 并对类型执行了 Find Usages。

唯一直接引用它的 UI 类型是 HeaderedItemsControl。所以任何扩展它的类型都是兼容的。其中包括 MenuItem, ToolBar, TreeViewItem 和一些 WF4 设计器类型。

MenuItems 由菜单使用,其中包括功能区和上下文菜单。工具栏就是这样。他们不太令人兴奋。并且TreeViewItems被TreeViews使用。

您可以自己获取一份 JustDecompile 的副本,或通过浏览 http://referencesource.microsoft.com/

来进一步研究不同的类型