WPF 分层数据模板树视图
我想使用 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 绑定到 CriteriumGroupHierarchicalDataTemplate
。 问题是,我似乎无法正确处理 - 我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
采用这样的方法是有效的:
这是一种简单的开始方法,一旦你让它发挥作用,你就可以四处移动东西并将它们作为资源部分中的样式和模板。
Taking an approach like this works:
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.