无法使用Galasoft MVVM显示TreeView项目

时间:2018-05-09 08:43:45

标签: c# wpf treeview

我在使用WPF和Galasoft MVVM工具包获取简单的TreeView以显示项目时遇到了一些困难。我必须遗漏一些简单的东西,但我无法找到它。

目前我想要的是创建一组几个节点并显示它们。我甚至没有写过任何RelayCommands或其他任何实质性内容,所以不用担心。此外,我认识到我可能需要在某处包含HierarchicalDataTemplate - 可能会替换" TreeView.ItemTemplate"一部分。

任何人都可以指出一些我无法看到的明显错误吗?任何帮助将不胜感激!

编辑:我已更新代码以修复缺少子节点列表的问题,但仍未显示任何内容。

MainWindow.xaml:

<Window x:Class="Analyzer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.galasoft.ch/ignore"
        mc:Ignorable="d ignore"
        Height="495.333"
        Width="700"
        Title="Analyzer"
        DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid x:Name="LayoutRoot" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">

        <TextBlock FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextWrapping="Wrap" />
        <TreeView ItemsSource="{Binding AllItems}" x:Name="MainTreeView" HorizontalAlignment="Left" Height="408" Margin="10,10,0,0" VerticalAlignment="Top" Width="662">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

        <TextBox x:Name="SearchTextbox" HorizontalAlignment="Left" Height="23" Margin="10,423,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="545"/>
        <Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Left" Margin="560,423,0,0" VerticalAlignment="Top" Width="112" Height="23"/>

    </Grid>
</Window>

MainViewModel.cs:

using GalaSoft.MvvmLight;
using Analyzer.Model;

namespace Analyzer.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {

        private Node _allItems;
        public Node AllItems
        {
            get
            {
                return _allItems;
            }
            set
            {
                _allItems = value;
                RaisePropertyChanged("AllItems");
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            Node root = new Node("Root", null);
            Node child1 = new Node("Child 1", root);
            Node grandchild = new Node("Grandchild 1", child1);
            Node child2 = new Node("Child 2", root);

            root.AddChild(child1);
            root.AddChild(child2);
            child1.AddChild(grandchild);

            AllItems = root;
            RaisePropertyChanged("AllItems");
        }

    }
}

节点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Analyzer.Model
{
    public class Node
    {


        public Node()
        {

        }

        public Node(string name, Node parent)
        {
            Name = name;
            Parent = parent;
            Children = new List<Node>();
        }

        public void AddChild(Node child)
        {
            _children.Add(child);
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        private Node _parent;
        public Node Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
            }
        }

        private List<Node> _children;
        public List<Node> Children
        {
            get
            {
                return _children;
            }
            set
            {
                _children = value;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你需要一个像这样的HierarchicalDataTemplate;

    <TreeView ItemsSource="{Binding AllItems}" x:Name="MainTreeView" HorizontalAlignment="Left" Height="408" Margin="10,10,0,0" VerticalAlignment="Top" Width="662">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name}" />  
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

此外,您需要重构您的节点类型,以便节点有子节点,例如

public class Node
{
    public IEnumerable<Node> Children { get; set; }
    public string Name { get; set; }
}

最后,您必须更改在视图模型中构建树的方式,并确保在AllItems上更改了属性。

答案 1 :(得分:0)

我认为你是对的,其中一个存在问题;在编辑过程中,我必须覆盖或删除重要的内容。我发现创建一个新项目时,我解决问题的问题要少得多。

我还必须对我的XAML代码进行以下更改:

<TreeView x:Name="treeView" ItemsSource="{Binding AllItems.Children}" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="188">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

出于某种原因,我无法将其直接绑定到AllItems - 我必须将顶级绑定到子级。这省略了我的根节点,但其他一切似乎都有效,所以我现在只是使用它。谢谢你的帮助!