Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-创建树视图_Wpf_Treeview - Fatal编程技术网

WPF-创建树视图

WPF-创建树视图,wpf,treeview,Wpf,Treeview,我有一个可以表示层次结构的类型字段列表: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; } publi

我有一个可以表示层次结构的类型字段列表: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; }
}
公共类字段
{
公共字段(字符串名称、字符串值)
{
this.Name=Name;
这个。值=值;
}
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共IList子字段{get;set;}
}
如何将MyFields绑定到TreeView

编辑:
我忘了,我想在单击项目时在消息框中显示值。

将TreeView项目资源设置为要绑定的属性。

您可以创建一个属性,该属性应放在TreeView的资源中或更高级别,确保将
数据类型设置为类以使其应用

比如说:

<HierarchicalDataTemplate DataType="{x:Type data:Field}"
                          ItemsSource="{Binding SubFields}">
    <ContentControl MouseDoubleClick="TreeViewItem_MouseDoubleClick">
        <TextBlock Text="{Binding Name}"/>
    </ContentControl>
</HierarchicalDataTemplate>
您还需要一个根元素列表,您可以将TreeView本身的
ItemsSource
绑定到该列表。

的可能副本
private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    Field field = (sender as FrameworkElement).DataContext as Field;
    MessageBox.Show(field.Value.ToString());
}