[英]Why doesn't the following WPF treeview work?
自從我使用WPF以來已經有很長時間了,現在,我需要在WPF上做一個小項目,我在制作一個簡單的數據綁定樹視圖時遇到了一個非常奇怪的問題。
到目前為止的主窗口:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
populateTreeview();
}
private XMLDataNode tree;
public XMLDataNode TreeRoot
{
get { return tree; }
set
{
tree = value;
NotifyPropertyChanged("TreeRoot");
}
}
//Open the XML file, and start to populate the treeview
private void populateTreeview()
{
{
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.Wait;
string filePath = System.IO.Path.GetFullPath("TestXML.xml");
TreeRoot = RXML.IOManager.GetXMLFromFile(filePath).Last();
}
catch (XmlException xExc)
//Exception is thrown is there is an error in the Xml
{
MessageBox.Show(xExc.Message);
}
catch (Exception ex) //General exception
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.AppStarting; //Change the cursor back
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
我正在使用的數據類將XML節點加載到自身中:
public class XMLDataNode
{
public XmlNode node;
public string Title;
public List<XMLDataNode> Children;
public XMLDataNode(XmlNode XMLNode)
{
this.node = XMLNode;
this.Title = node.ToString();
Children = new List<XMLDataNode>();
foreach (XmlNode item in XMLNode.ChildNodes)
{
Children.Add(new XMLDataNode(item));
}
}
主窗口XAML:
<Window x:Class="RotemXMLEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1000" x:Name="me">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TreeView Grid.Row="1" x:Name="treeView" Background="White" ItemsSource="{Binding ElementName=me, Path=TreeRoot}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Title}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
當前輸出是一個空的樹視圖,其中沒有任何內容,即使databinded對象中已填充信息。
我意識到這可能是一個愚蠢的錯誤,但我似乎真的找不到錯誤,幫助?
您可以在實現IEnumerable的集合中打開此屬性嗎?
public XMLDataNode TreeRoot
例如:
public XMLDataNode[] TreeRoots
TreeView的ItemsSource期望實現IEnumerable的類型
這對我有用:
<TreeView Grid.Row="1" x:Name="treeView" Background="White" ItemsSource="{Binding ElementName=me, Path=TreeRoots}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Title}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
private XMLDataNode[] treeRoots;
public XMLDataNode[] TreeRoots
{
get { return treeRoots; }
set
{
treeRoots = value;
NotifyPropertyChanged("TreeRoots");
}
}
private void populateTreeview()
{
{
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.Wait;
string filePath = System.IO.Path.GetFullPath("TestXML.xml");
TreeRoot = RXML.IOManager.GetXMLFromFile(filePath).Last();
TreeRoots = new[] { TreeRoot };
}
catch (XmlException xExc)
我找到了解決方案。
即使字段是公共字段,WPF數據綁定也只能與屬性一起使用(我忘記了),而字段卻不起作用,將所有數據綁定的字段更改為實際屬性可以解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.