2011-01-31 132 views
0

比方说,我有这样的事情:WPF的TreeView DataTemplate中

public class TopicFolder 
    { 
     #region Constants and Fields 

     private readonly List<TopicInfo> folderContent; 

     private readonly List<TopicFolder> subFolders; 

     #endregion 

... 
    } 

如何实现这种类型的数据模板?目前我有:

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

但是这并不显示任何文件夹内容。看起来第二个模板的DataType应该是本地的:TopicFolder,但这是WPF不允许的。

有什么建议吗?

UPD:TreeView控件绑定到的ObservableCollection < TopicFolder>是这样的:

ItemsSource="{Binding Path=Folders}" 

P.S:这绝对不是一个私人/公共/性能问题。我拥有相应的公开属性。输出中没有绑定错误,它只是不显示任何FolderContent项目。

+0

究竟是什么您的错误信息? – 2011-01-31 17:12:11

+0

并不会错过封装私有字段的属性? – 2011-01-31 17:13:21

回答

1

编辑:

要显示两个子文件夹和内容的一个可以使用一个MultiBinding或者,如果你不介意的文件夹和内容可以按照一定的顺序,我建议使用composite pattern出现,因为您可以删除SubFolders和FolderContent,并将其替换为实现复合接口的对象集合(请阅读wiki文章)。

创建一个属性来合并两个集合,所以你可以绑定到它,这是不好的做法。

例复合模式:

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 
{ 
    //... 
}