[英]WPF treeview datatemplate
Let's say I have something like this: 假设我有这样的事情:
public class TopicFolder
{
#region Constants and Fields
private readonly List<TopicInfo> folderContent;
private readonly List<TopicFolder> subFolders;
#endregion
...
}
How do I implement a data template for such type? 如何为此类数据实现数据模板? Currently I have: 目前我有:
<HierarchicalDataTemplate DataType="{x:Type local:TopicFolder}" ItemsSource="{Binding SubFolders}" >
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TopicInfo}" ItemsSource="{Binding FolderContent}">
<TextBlock Text="{Binding TopicName}"/>
</HierarchicalDataTemplate>
But this does not show any folder content. 但这不会显示任何文件夹内容。 It seems that second's template DataType should be local:TopicFolder, but this is not allowed by WPF. 似乎第二个模板的DataType应该是local:TopicFolder,但是WPF不允许这样做。
Any suggestions? 有什么建议么?
UPD : TreeView is bound to ObservableCollection<TopicFolder> this way: UPD:TreeView通过以下方式绑定到ObservableCollection <TopicFolder>:
ItemsSource="{Binding Path=Folders}"
PS: It is definitely not a private/public/properties problem. PS:绝对不是私人/公共/财产问题。 I have corresponding public properties for posted fields. 我具有已发布字段的相应公共属性。 No binding errors in output, it is just not showing any FolderContent items. 输出中没有绑定错误,只是不显示任何FolderContent项目。
Edit: 编辑:
To show both sub-folders and content one can either use a MultiBinding
or if you don't mind that folders and content can appear in a certain order I'd suggest using the composite pattern , for that you remove your SubFolders and FolderContent and replace it with a collection of objects which implement the composite interface (read the wiki article). 要同时显示子文件夹和内容,可以使用MultiBinding
或者如果您不介意文件夹和内容可以按一定顺序显示,我建议您使用复合模式 ,因为您要删除SubFolders和FolderContent并替换它带有实现复合接口的对象集合(请参阅Wiki文章)。
Creating a property to merge the two collections, so you can bind to it, is bad practice. 创建一个属性来合并两个集合以便可以绑定到它是一种不好的做法。
Example for composite pattern: 复合模式示例:
public interface ITopicComposite
{
// <Methods and properties folder and content have in common (e.g. a title)>
// They should be meaningful so you can just pick a child
// out of a folder and for example use a method without the
// need to check if it's another folder or some content.
}
public class TopicFolder : ITopicComposite
{
private readonly ObservableCollection<ITopicComposite> children = new ObservableCollection<ITopicComposite>();
public ObservableCollection<ITopicComposite> Children
{
get { return children; }
}
//...
}
public class TopicInfo : ITopicComposite
{
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.