0

假设我有这样的事情:

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应该是local:TopicFolder,但是WPF不允许这样做。

有什么建议么?

UPD:TreeView 以这种方式绑定到 ObservableCollection<TopicFolder> :

ItemsSource="{Binding Path=Folders}"

PS:这绝对不是私人/公共/财产问题。对于已发布的字段,我有相应的公共属性。输出中没有绑定错误,只是没有显示任何 FolderContent 项。

4

1 回答 1

1

编辑:

要同时显示子文件夹和内容,可以使用 aMultiBinding或者如果您不介意文件夹和内容可以按特定顺序显示,我建议您使用复合模式,因为您可以删除您的 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
{
    //...
}
于 2011-01-31T17:14:21.357 回答