gpt4 book ai didi

c# - HierachicalDataTemplate和具有ItemsControl的DataTemplate之间的区别

转载 作者:行者123 更新时间:2023-12-03 10:45:01 25 4
gpt4 key购买 nike

我想知道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
}

TeamManagerViewModel.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");
}
}
}

TeamViewModel.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");
}
}
}

因此,结果是窗口中出现了名称列表。但是,如果我用注释的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由TreeViews使用。从documentation

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



没有专门显示分层数据的 ItemsControls将不会使用该模板。我不能以另一个控件为名...可能是菜单?没有把握。但是在 TreeView中使用此模板类型意味着您不必在每个不代表叶子的模板中添加子 TreeView。这样可以节省一些时间。

并非旨在使用它们的控件绝对不会在您的引用资料中寻找它们。

因为您似乎担心它的实际使用位置,所以我打开JustDecompile并对该类型进行了“查找用法”。

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

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

您可以自己获取JustDecompile副本或浏览 http://referencesource.microsoft.com/,以对不同类型进行进一步研究

关于c# - HierachicalDataTemplate和具有ItemsControl的DataTemplate之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30245842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com