WPF-创建树视图
我有可以表示层次结构的类型字段列表:List MyFields
public class Field
{
public Field(string name, string value)
{
this.Name = name;
this.Value = value;
}
public string Name { get; set; }
public string Value { get; set; }
public IList<Field> SubFields { get; set; }
}
如何将 MyFields 绑定到 TreeView?
编辑: 我忘了,我想例如。单击该项目时在消息框中显示该值。
I have list of type field that can represent a hierarchy: List MyFields
public class Field
{
public Field(string name, string value)
{
this.Name = name;
this.Value = value;
}
public string Name { get; set; }
public string Value { get; set; }
public IList<Field> SubFields { get; set; }
}
How can i bind MyFields to a TreeView?
EDIT:
I forgot, i want to eg. show the value in a message box when clicking on the item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 TreeViews ItemsSource 设置为要绑定的属性。
Set the TreeViews ItemsSource to the Property you want to bind with.
您可以创建一个
HierarchicalDataTemplate
应放置在 TreeView 的资源中或更高级别的资源中,请确保将DataType
设置为您的类以使其应用。例如:
您还需要一个根元素列表,可以将 TreeView 本身的
ItemsSource
绑定到该列表。You can create a
HierarchicalDataTemplate
which should be placed in the resources of your TreeView or at an higher level, make sure to set theDataType
to your class to make it apply.Something like this for example:
You also need a root-elements list to which you can bind the
ItemsSource
of the TreeView itself.