我对MVVM相当陌生。除了我的treeView,我的应用程序其余所有内容都可以正常工作。
我一直在使用这个example
我有一个MessageTree对象,如下所示:
public class MessageTree
{
public MessageTree(string message, MessageSeverity severity)
{
Message = message;
MessageSeverity = severity;
}
public MessageSeverity MessageSeverity { get; set; }
public string Message { get; set; }
public List<MessageTree> Children { get; set; }
public MessageTree AddChild(string message, MessageSeverity severity)
{
var child = new MessageTree(message, severity);
return AddChild(child);
}
public MessageTree AddChild(MessageTree child)
{
if (Children == null)
{
Children = new List<MessageTree>();
}
Children.Add(child);
return child;
}
public void Add(List<MessageTree> nodes)
{
if(Children == null)
{
Children = new List<MessageTree>();
}
Children.AddRange(nodes);
}
public override string ToString()
{
return ToString(0);
}
public string ToString(int depth)
{
var ret = new StringBuilder();
ret.Append(new string('\t', depth));
ret.AppendLine(Message);
if (Children != null)
{
foreach(var child in Children)
{
ret.AppendLine(child.ToString(depth + 1));
}
}
return ret.ToString();
}
public void ToConsole()
{
ToConsole(0);
}
public void ToConsole(int depth)
{
var tabs = new string('\t', depth);
Console.WriteLine($"{tabs}{Message}");
if (Children != null)
{
foreach (var child in Children)
{
child.ToConsole(depth + 1);
}
}
}
public MessageSeverity InheritedSeverity
{
get
{
if (Children == null)
{
return MessageSeverity;
}
var errors = Children.Where(c => c.InheritedSeverity == MessageSeverity.Error);
if (errors.Any())
{
return MessageSeverity.Error;
}
var warnings = Children.Where(c => c.InheritedSeverity == MessageSeverity.Warning);
if (warnings.Any())
{
return MessageSeverity.Warning;
}
return MessageSeverity.None;
}
}
}
然后,在我的viewModel中
private MessageTree _tree;
public MessageTree Tree
{
get => _tree;
set
{
_tree = value;
OnPropertyChanged(nameof(Tree));
}
}
public void Validate()
{
Tree = Validate(Remittance) ?? throw new ArgumentNullException($"Validate(Remittance)");
}
...
我的观点:
<UserControl x:Class="ConveyorFrontEnd.Controls.RemittanceValidator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ConveyorFrontEnd.Controls"
xmlns:common="clr-namespace:Teragra.Common;assembly=Teragra.Common"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TreeView ItemsSource="{Binding Tree}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type common:MessageTree}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=Message}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<!--<Button Grid.Column="0" Content="Validate" Command="{Binding Path=ValidateCommand}"/>-->
</Grid>
调试我的项目时,Tree对象将至少填充一项,而Children中的一项将被填充。但是,视图上什么也不会显示,输出中也不会显示任何内容。当我将视图的绑定更改为不存在的绑定绑定之类的东西时,它指定不能显示TreeBreakHere,因此很明显可以绑定。我还已将textBlock更改为不是绑定,而是更改为诸如Text =“ TextGoesHere”之类的常量,但仍不显示任何内容。
我相当有信心,我的问题在于我尝试指定树的结构的方式,但是似乎无法找出我要去哪里。谁能向正确的方向推动我?
答案 0 :(得分:0)
问题出在treeView中的ItemSource。
我绑定到一个元素,就像这样:
<TreeView ItemsSource="{Binding Tree}">
绑定应该是可枚举的,而不是单个元素