我已经创建了一个应用程序,用于加载特殊格式的XML文件并通过WPF GUI编辑其字段。但是,由于是C#的新手,所以我在编写MVVM模式时并不了解它,因此我采取了快速而又肮脏的方法,即仅对后面的代码进行编码。但是,重新构造后,我似乎无法再更新以前正在工作的树视图了。
以下是相关的XAML:
<UserControl x:Class="TableBuilder.Views.XmlView"
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:views="clr-namespace:TableBuilder.Views"
xmlns:viewmodels="clr-namespace:TableBuilder.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<viewmodels:XmlViewModel />
</UserControl.DataContext>
<DockPanel>
<TreeView x:Name="XmlTree" SelectedItemChanged="XmlView_SelectedItemChanged">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Margin="2" Source="../Images/Red_Bullet.png" />
<TextBlock VerticalAlignment="Center" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</DockPanel>
</UserControl>
下面是代码:
using System;
using System.Windows;
using System.Windows.Controls;
using TableBuilder.Helpers;
namespace TableBuilder.Views
{
/// <summary>
/// Interaction logic for XmlView.xaml
/// </summary>
public partial class XmlView : UserControl
{
RelayCommand selectedItemChanged { get; set; }
public XmlView()
{
InitializeComponent();
}
private void XmlView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//((MainViewModel)this.DataContext).SelectedItemChangedCommand = (RelayCommand)sender;
Console.WriteLine("XmlView selected item changed event triggered");
}
}
}
这是视图模型:
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Xml;
using TableBuilder.Helpers;
using TableBuilder.Models;
namespace TableBuilder.ViewModels
{
class XmlViewModel : ViewModelBase
{
private TreeView _xmlTree;
public TreeView XmlTree
{
get { return _xmlTree; }
set
{
_xmlTree = value;
OnPropertyChanged("XmlTree");
}
}
public XmlViewModel()
{
xmlModel = new XmlModel();
TreeView XmlTree = new TreeView();
TreeViewItem node = new TreeViewItem();
node.Header = "Test";
XmlTree.Items.Add(node);
xmlModel.PropertyChanged += XmlModel_CurrentChanged;
}
private void XmlModel_CurrentChanged(object sender, EventArgs e)
{
TreeViewItem xmlTreeItems = GetTreeViewItems();
TreeView XmlTree = new TreeView();
XmlTree.Items.Add(xmlTreeItems);
}
private TreeViewItem GetTreeViewItems()
{
// Find the filename of the XML document
Uri uri = new Uri(xmlModel.XmlDoc.BaseURI);
string filename = "";
if (uri.IsFile)
{
filename = System.IO.Path.GetFileName(uri.LocalPath);
}
// Populate the TreeView
TreeViewItem TitleNode = new TreeViewItem()
{
Header = filename,
Tag = "Title Node",
};
PopulateTree(TitleNode, xmlModel.XmlDoc.ChildNodes);
return TitleNode;
}
private void PopulateTree(TreeViewItem parent, XmlNodeList nodes)
{
TreeViewItem item;
Dictionary<string, string> dict;
bool isSmallestChild;
foreach (XmlNode node in nodes)
{
// Ignore all comments in the XML document
if (!node.Name.Contains("#comment"))
{
// Add to TreeView
item = toTreeViewItem(node);
isSmallestChild = false;
dict = new Dictionary<string, string>();
foreach (XmlNode child in node.ChildNodes)
{
if (child.Name.Contains("type") ||
child.Name.Contains("value") ||
child.Name.Contains("units"))
{
isSmallestChild = true;
}
if (isSmallestChild)
{
dict.Add(child.Name, child.InnerText);
}
}
if (!isSmallestChild)
{
PopulateTree(item, node.ChildNodes);
}
else
{
item.Tag = dict;
}
parent.Items.Add(item);
}
}
}
private TreeViewItem toTreeViewItem(XmlNode node)
{
TreeViewItem item = new TreeViewItem()
{
Header = node.Name.Contains("#") ? node.InnerText : node.Name,
};
return item;
}
}
}
这是模型:
using System.Xml;
using TableBuilder.Helpers;
namespace TableBuilder.Models
{
public class XmlModel : ModelBase
{
private XmlDocument _xmlDoc;
public XmlDocument XmlDoc
{
get { return _xmlDoc; }
set
{
_xmlDoc = value;
OnPropertyChanged("XmlDoc");
}
}
public XmlModel()
{
XmlDoc = new XmlDocument();
}
}
}
这是viewmodel基类:
using System.ComponentModel;
using TableBuilder.Models;
namespace TableBuilder.Helpers
{
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
static protected RedModel redModel { get; set; }
static protected XmlModel xmlModel { get; set; }
static protected MainModel mainModel { get; set; }
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
最后,这是模型基类:
using System.ComponentModel;
namespace TableBuilder.Helpers
{
public class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我知道代码中有很多草率的做法;这是我的第一个C#/ WPF应用程序。为了帮助限制此问题的范围,我只想知道为什么不能从我的视图模型设置树视图数据。我曾尝试在Google周围闲逛,但没有成功。我应该注意,我能够用文本框替换树视图,并从视图模型中将数据设置为文本框。
答案 0 :(得分:0)
有关解决方案,请参见上面的注释部分。 elgonzo指出了在重新架构过程中发生的主要架构问题:视图模型不直接与UI元素交互,它们与XAML中绑定到UI元素的数据交互。
另一个缺失的部分是由于我再次在mainviewmodel中实例化我的视图模型以创建适当的美感,但是,这破坏了子窗口的数据上下文,这意味着我只能在子窗口中操作数据。构造函数。解决方案是在更改UI方面而不是视图模型时实例化VIEWS。这样做会保留数据上下文。
编辑: 我的XmlPane没有发送有关其正在更新的通知。实例化视图模型上的视图是这样做的结果,而不是我上面提到的。