1

我有两节课

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; }
}

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

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

我用递归树做到了

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

我用 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

4

2 回答 2

2

稍微修改一下您的代码:实现根视图模型 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>
于 2013-08-28T12:33:27.457 回答
1

首先,如果您还希望显示根节点,则需要将 ItemsSource 设置为包含根节点的集合。

至于没有显示的叶子......你需要一个 DataTemplate,而不是 HierarchicalDataTemplate。我怀疑绑定错误会阻止它正确渲染,因为叶子没有子集合。

刚刚注意到这是一个家庭作业问题,所以我不会再继续下去了。查看绑定错误(启用 WPF 跟踪以在输出窗口中查看它们)并尝试从那里找出它。

于 2013-08-28T11:35:42.363 回答