gpt4 book ai didi

c# - wpf mvvm递归树

转载 作者:行者123 更新时间:2023-12-03 10:23:27 24 4
gpt4 key购买 nike

我有两个类

public class leaf
{
public string name { get; set; }
}


public class node
{
public ObservableCollection<node> nodeList = new ObservableCollection<node>();
public ObservableCollection<leaf> leafList = new ObservableCollection<leaf>();
public ObservableCollection<node> prop_nodeList { get { return nodeList; } set { nodeList = value; } }
public ObservableCollection<leaf> prop_leafList { get { return leafList; } set { leafList = value; } }

public string name { get; set; }
}

正如你可以看到它的工作就像道路或树木。节点可以拥有另一个节点和叶子的信息。

我想在像这样格式化的用户控制树中显示

i make it with using Recursiv tree

nethead 是一个主节点,它有两个另外的节点(b,a)。节点 a 有两个节点(b,c)和 2 个叶子(a1,a2)。但我不使用 mvvm。当我用 mvvm 做它时,它看起来像

i make it with mvvm

从 C# 我只做
this.DataContext = mainNode; // its node which hold everething (its named netHead)

从 xaml 它
<Grid>
<TreeView ItemsSource="{Binding prop_nodeList}">
<TreeView.Resources>

<HierarchicalDataTemplate DataType="{x:Type local:node}" ItemsSource="{Binding prop_nodeList}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:leaf}" ItemsSource="{Binding prop_leafList}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>


</TreeView.Resources>
</TreeView>
</Grid>

你明白我想做什么了吗?我想在 TreeView 中制作双 treeItem,但它不起作用:(
请帮忙,它是 Ant 算法的项目,我想在教室里做最好的 gui

最佳答案

稍微修改一下您的代码:实现 Root View 模型 NodesData 只是为了演示和创建启动数据。还添加了属性 Children 以具有节点子级的单个属性。并且还稍微修改了 xaml 以使用单个分层数据模板。似乎它有效。

  public class leaf
{
public string name { get; set; }
}

public class node
{
public string name { get; set; }
public ObservableCollection<node> nodeList = new ObservableCollection<node>();
public ObservableCollection<leaf> leafList = new ObservableCollection<leaf>();
public ObservableCollection<node> prop_nodeList { get { return nodeList; } set { nodeList = value; } }
public ObservableCollection<leaf> prop_leafList { get { return leafList; } set { leafList = value; } }

public ObservableCollection<object> Children
{
get
{
var children = prop_nodeList.OfType<object>();
return new ObservableCollection<object>(children.Concat(prop_leafList.OfType<object>()));
}
}

}

public class NodesData
{
public ObservableCollection<node> RootSource { get; set; }

public NodesData()
{
var rootNode = new node() { name = ">>>Head Node<<<<" };


var b = new node() { name = "b" };
var c = new node() { name = "c" };
var a = new node()
{
name = "a",
prop_nodeList = new ObservableCollection<node>() { b, c },
prop_leafList = new ObservableCollection<leaf>() { new leaf() { name = "a1" }, new leaf() { name = "a2" } }
};
rootNode.prop_nodeList = new ObservableCollection<node>() { b, a };

RootSource = new ObservableCollection<node>() { rootNode };
}
}

}

XAML:
    <Grid>
<Grid.Resources>
<wpfApplication1:NodesData x:Key="dataSource"/>
</Grid.Resources>
<TreeView ItemsSource="{Binding RootSource}" DataContext="{StaticResource dataSource}">
<TreeView.Resources>

<HierarchicalDataTemplate DataType="{x:Type wpfApplication1:node}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type wpfApplication1:leaf}" >
<TextBlock Text="{Binding Path=name}"/>
</DataTemplate>



</TreeView.Resources>
</TreeView>
</Grid>

关于c# - wpf mvvm递归树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com