WPF 分层数据模板树视图

发布于 2024-11-04 21:25:48 字数 3502 浏览 3 评论 0原文

我想使用 TreeView 显示一些对象。 我使用的类定义如下:

public abstract class QueryPart
{
    private static Random _random;
    protected static Random Random { get { return _random??(_random=new Random(654)); } }
    public string Name { get; set; }
    public QueryPart()
    {
        this.Name = this.GetType().Name + " " + Random.Next();
    }       
}

public abstract class Criterium : QueryPart
{        
    object ParamValue { get; set; }
    protected Criterium()
    {
        ParamValue = Random.Next();
    }
}

public sealed class Criterium<T> : QueryPart
{             
}

public abstract class CriteriumGroup : QueryPart
{
    public ObservableCollection<QueryPart> Childeren { get; private set; }
    public CriteriumGroup()
    {
        Childeren = new ObservableCollection<QueryPart>();
    }
}

public sealed class CriteriumGroup<T> : CriteriumGroup
{        
}

public static class Helper
{
    public static CriteriumGroup<T> GetDefaultCriteriumGroup<T>()
    {
        var master = new CriteriumGroup<T>();
        master.Childeren.Add(new Criterium<T>());
        var sub = new CriteriumGroup<T>()
        {
            Childeren =
                {
                    new Criterium<T>(),
                    new Criterium<T>(),
                    new Criterium<T>(),
                    new CriteriumGroup<T>() 
                    {
                        Childeren =
                        {
                            new Criterium<T>(), 
                            new Criterium<T>(), 
                            new Criterium<T>()
                        }
                    },
                    new Criterium<T>()
                }
        };
        master.Childeren.Add(sub);
        sub = new CriteriumGroup<T>()
        {
            Childeren =
                {
                    new Criterium<T>(),
                    new CriteriumGroup<T>() 
                    {
                        Childeren =
                        {
                            new Criterium<T>(), 
                            new Criterium<T>(), 
                            new Criterium<T>()
                        }
                    },
                }
        };
        master.Childeren.Add(sub);
        return master;
    }
}

我将 TreeView 绑定到 CriteriumGroup的实例,并使用 HierarchicalDataTemplate。 问题是,我似乎无法正确处理 - 我的 TreeView.Items.Count 始终为 0

这就是我到目前为止所得到的:

<TreeView x:Name="treeview" ItemsSource="{Binding Path=RootCriteriumGroup}">
        <TreeView.Resources>
            <DataTemplate DataType="{x:Type DynamicLinq:QueryPart}">
                <local:QueryPartModelView2/>
            </DataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type DynamicLinq:Criterium}" ItemsSource={Binding Path=Childeren}>
                <local:QueryPartModelView2/>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate 
                              DataType="{x:Type DynamicLinq:CriteriumGroup}" 
                              ItemsSource="{Binding Path=Childeren}">
                <local:QueryPartModelView2/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

我做错了什么?

I have some objects I would like to display using a TreeView.
The classes I use are defined like this:

public abstract class QueryPart
{
    private static Random _random;
    protected static Random Random { get { return _random??(_random=new Random(654)); } }
    public string Name { get; set; }
    public QueryPart()
    {
        this.Name = this.GetType().Name + " " + Random.Next();
    }       
}

public abstract class Criterium : QueryPart
{        
    object ParamValue { get; set; }
    protected Criterium()
    {
        ParamValue = Random.Next();
    }
}

public sealed class Criterium<T> : QueryPart
{             
}

public abstract class CriteriumGroup : QueryPart
{
    public ObservableCollection<QueryPart> Childeren { get; private set; }
    public CriteriumGroup()
    {
        Childeren = new ObservableCollection<QueryPart>();
    }
}

public sealed class CriteriumGroup<T> : CriteriumGroup
{        
}

public static class Helper
{
    public static CriteriumGroup<T> GetDefaultCriteriumGroup<T>()
    {
        var master = new CriteriumGroup<T>();
        master.Childeren.Add(new Criterium<T>());
        var sub = new CriteriumGroup<T>()
        {
            Childeren =
                {
                    new Criterium<T>(),
                    new Criterium<T>(),
                    new Criterium<T>(),
                    new CriteriumGroup<T>() 
                    {
                        Childeren =
                        {
                            new Criterium<T>(), 
                            new Criterium<T>(), 
                            new Criterium<T>()
                        }
                    },
                    new Criterium<T>()
                }
        };
        master.Childeren.Add(sub);
        sub = new CriteriumGroup<T>()
        {
            Childeren =
                {
                    new Criterium<T>(),
                    new CriteriumGroup<T>() 
                    {
                        Childeren =
                        {
                            new Criterium<T>(), 
                            new Criterium<T>(), 
                            new Criterium<T>()
                        }
                    },
                }
        };
        master.Childeren.Add(sub);
        return master;
    }
}

I would bind the TreeView to an instance of CriteriumGroup<T>, and use a HierarchicalDataTemplate.
The problem is, I can't seem to get it right - my TreeView.Items.Count is always 0.

This is what I have so far:

<TreeView x:Name="treeview" ItemsSource="{Binding Path=RootCriteriumGroup}">
        <TreeView.Resources>
            <DataTemplate DataType="{x:Type DynamicLinq:QueryPart}">
                <local:QueryPartModelView2/>
            </DataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type DynamicLinq:Criterium}" ItemsSource={Binding Path=Childeren}>
                <local:QueryPartModelView2/>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate 
                              DataType="{x:Type DynamicLinq:CriteriumGroup}" 
                              ItemsSource="{Binding Path=Childeren}">
                <local:QueryPartModelView2/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-11-11 21:25:48

采用这样的方法是有效的:

<TreeView Name="treeView1" ItemsSource="{Binding MyEnumerableProperty}" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding MyImageUri}"  />
                <TextBlock x:Name="someName" Text="{Binding MyNodeText}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这是一种简单的开始方法,一旦你让它发挥作用,你就可以四处移动东西并将它们作为资源部分中的样式和模板。

Taking an approach like this works:

<TreeView Name="treeView1" ItemsSource="{Binding MyEnumerableProperty}" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding MyImageUri}"  />
                <TextBlock x:Name="someName" Text="{Binding MyNodeText}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

This is a simple way to start, once you have it working you can then look to move things around and have them as Styles and Templates in Resource sections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文