У меня возникли трудности с получением простого TreeView для отображения элементов с использованием WPF и инструментария Galasoft MVVM. Я, должно быть, упускаю что-то простое, но я просто не могу найти это.
Все, что я сейчас хочу, это создать набор из нескольких узлов и отобразить их. Я даже не дошел до написания каких-либо 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");
}
}
}
Node:
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;
}
}
}
}