HierachicalDataTemplate 和 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>

视图模型.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
}

团队经理视图模型.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");
}
}
}

团队视图模型.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");
}
}
}

因此,其结果是窗口中出现了名称列表。但是,如果我将TeamManagerViewModel的数据模板替换为注释的分层数据模板,则不会显示任何内容。

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

还有一个问题,我的样本和 MSDN 样本有什么区别(除了在分层数据模板中添加文本块之外,它们似乎和我做同样的事情)?

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

如果我的问题写得不好,也请原谅我,这是我第一次在SO上问一些东西。

谢谢:)

HierarchicalDataTemplates

由TreeViews使用。 从文档

视图可以通过绑定到数据源并使用分层数据模板对象来填充其树。

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

未设计为使用它们的控件绝对不会在您的资源中查找它们。


因为你似乎非常关心它的实际使用位置,所以我打开了 JustDecompile 并对类型进行了查找用法。

唯一直接引用它的 UI 类型是 HeaderedItemsControl。 因此,任何扩展此内容的类型都是兼容的。 其中包括菜单项、工具栏、树视图项和一些 WF4 设计器类型。

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

您可以通过自己获取JustDecompile的副本或浏览 http://referencesource.microsoft.com/来进一步研究

不同类型的

最新更新