怎样才可以有多种类型的孩子在一个单一的Silverlight TreeView的节点? [英] How can I have multiple types of children in a single Silverlight TreeView node?

查看:114
本文介绍了怎样才可以有多种类型的孩子在一个单一的Silverlight TreeView的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短Vesion:

我要显示的层次结构( TreeView控件)不同类型的项目,而我不知道如何做到这一点干净在Silverlight。在WPF中,这是简单的定义基于类型的模板( HierarchicalDataTemplate ),但是这个功能不可用在Silverlight。这似乎在Silverlight你有相同的模板应用到特定节点的所有孩子,所以你最终一次处理每一个可能的节点类型,适用于每一个节点一个怪物模板。

I have to display a hierarchy (TreeView) of items of different types, and am not sure how to do this cleanly in Silverlight. In WPF, it's straightforward to define templates (HierarchicalDataTemplate) based on types, but this feature isn't available in Silverlight. It seems in Silverlight you have to apply the same template to all children of a specific node, so you end up with once single monster template that handles every possible type of node, applied to every single node.

长的版本(带有示例):

要给出一个更具体的(但做作)的例子,考虑一个TreeView在不同的文件夹档案,每个档案可以包含照片,歌曲和其他档案。 。每个文件夹可包含若干子文件夹和档案

To give a more concrete (but contrived) example, consider a treeview of archives in various folders, where each archive can contain photos, songs, and other archives. Each folder may contain several subfolders and archives.

|-Folder
  |-Folder
    |-Folder
      |-Archive
        | Photo1
        | Photo2
        | Song1
        | Song2
        |-Archive
          | Photo1
          | Song1
  |-Archive
    | Photo1
    | Photo2
    | Photo3

在树中的每个类型(文件夹,档案,照片,歌)的显示方式不同。显而易见的解决方案似乎是创建一个 HierarchicalDataTemplate 每种类型的项目显示。不幸的是,我无法找到一个很好的办法做到这一点,因为它似乎你必须指定一个模板类型的所有节点的孩子(的ItemsSource = {绑定...}, ItemsTemplate = {StaticResource的TemplateForAllChildren} )。

Each type in the tree (Folder, Archive, Photo, Song) is displayed differently. The obvious solution seemed to be to create a HierarchicalDataTemplate for each type of item to display. Unfortunately, I can't find a good way to do this, because it seems that you have to specify a single template type for all of the children of a node (ItemsSource={Binding ...}, ItemsTemplate={StaticResource TemplateForAllChildren}).

这需求导致模板越滚越大......归档可以有照片,歌曲,档案儿童。因为一个模板必须适用于所有儿童,一个模板必须能够处理照片,歌曲,和档案。同样,一个文件夹的模板必须能够处理文件夹和档案,并存档模板现在有照片和停留在它的歌曲,所以这一切是一个巨大的模板,可以处理照片,歌曲,档案和文件夹结束。随着越来越多的类型增加,他们也得到集中到一个巨大的模板。

This requirement causes the template to snowball... an archive can have Photos, Songs, and Archives as children. Because a single template must be applied to all children, that one template must be able to handle Photos, Songs, and Archives. Similarly, a Folder's template must be able to handle Folders and Archives, and the Archive template now has Photos and Songs stuck in it, so it all ends up as one giant template that can handle Photos, Songs, Archives, and Folders. As more types are added, they also get lumped into the one huge template.

有什么办法干净地做到这一点,而不会产生一个巨大的模板(及相关节点视图模型?)作为不同类型添加到树

Is there any way to do this cleanly, without accumulating one giant template (and associated node viewmodel) as different types are added to the tree?

感谢

一些说明:

感谢您的答案,到目前为止,但我认为他们可能只是导致我回到原来的问题。我可能是误解了答案

Thanks for the answers so far, but I think they may just lead me back to the original issue. I may be misunderstanding the answer.

考虑TreeView控件显示:

Consider the TreeView showing:

有关的歌曲:一个滚动与艺术家文本框/标题和播放按钮

For Songs: a scrolling textbox with artist/title, and a play button

有关图片:缩略图和星级控

For Pictures: a thumbnail image, and a star rating control

有关档案:存档的图像,以进度条显示压缩

For Archives: An archive image, with a progress bar showing the compression

有关文件夹:显示文件夹名称的简单的标签

For Folders: A plain label showing the folder name

据我所知道的,实现这一目标的唯一方法是有一个包含滚动文本框,一个播放按钮,缩略图浏览器,一个明星的控制,图像控制,进度条和标签1巨型HierarchicalDataTemplate 。我然后只选择隐藏所有,但实际上适用于该节点的一个或两个控件。

As far as I can tell, the only way to achieve this is to have 1 giant HierarchicalDataTemplate containing a scrolling textbox, a play button, a thumbnail viewer, a star control, an image control, a progress bar, and a label. I'd then just selectively hide all but the one or two controls that actually apply to the node.

在WPF我可以用一个节点类型的模板相关联,因此每个节点可以使用一个合适的模板。我不知道是否有一种方法在Silverlight做到这一点。

In WPF I could associate templates with a node type, so each node could use an appropriate template. I'm wondering if there's a way to do this in Silverlight.

再次感谢!

推荐答案

好了,你为什么不尝试这样的事情?

Well, why don't you try something like this?

<sdk:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding Path=SubItems}">
    <TextBlock Text="{Binding Name}" Foreground="{Binding ForegroundColor}" />
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="FilesDataTemplate" ItemsSource="{Binding Path=SubItems}" ItemTemplate="{StaticResource ChildTemplate}">
    <TextBlock Text="{Binding Name}" Foreground="{Binding ForegroundColor}" />
</sdk:HierarchicalDataTemplate>



节点类



Node Class

public class Node
{
    public string Name { get; set; }
    public ObservableCollection<Node> SubItems { get; set; }
    public SolidColorBrush ForegroundColor { get; set; }

    public Node(string name, Color foregroundColor, params Node[] items)
    {
        this.Name = name;
        this.SubItems = new ObservableCollection<Node>(items);
        this.ForegroundColor = new SolidColorBrush(foregroundColor);
    }
}



示例数据



Example Data

public partial class MainPage : UserControl
{
    public ObservableCollection<Node> Nodes { get; set; }

    public MainPage()
    {
        InitializeComponent();

        this.Nodes = new Node("Root", Colors.Blue,
                             new Node("File1", Colors.Black),
                             new Node("File2", Colors.Black),
                             new Node("Archive1", Colors.Red,
                                        new Node("File3", Colors.Magenta),
                                        new Node("File4", Colors.Magenta))
                             ).SubItems;

        treeView1.DataContext = this;
    }
}

在你的情况,也许可以帮助一个接口(inode对例如),有对造型节点的所有属性(如ForegroundColor,或其他),将每种类型的子类(归档,照片,音乐)来实现。

In your case maybe could help an interface (INode for example) that has all the properties for styling nodes (like ForegroundColor, or whatever) that will be implemented by each type of subclass (Archive, Photo, Music).

希望这有助于。

这篇关于怎样才可以有多种类型的孩子在一个单一的Silverlight TreeView的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆