为什么以下 WPF 树视图不起作用?
Why doesn't the following WPF treeview work?
自从我使用 WPF 以来已经有很长时间了,现在,需要在它上面做一个小项目,我在制作一个简单的数据绑定树视图时遇到了一个非常奇怪的问题。
目前主要window:
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));
}
}
我正在使用的数据 class 将 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));
}
}
主要window 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>
当前输出是一个空的树视图,其中没有任何内容,即使数据绑定对象充满了信息。
我意识到这可能是一个愚蠢的错误,但我似乎真的找不到错误,求助?
你能在实现 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 数据绑定只能(我忘记了)属性,而不是字段,即使字段是 public,将所有数据绑定字段更改为实际属性可以解决这个问题。
自从我使用 WPF 以来已经有很长时间了,现在,需要在它上面做一个小项目,我在制作一个简单的数据绑定树视图时遇到了一个非常奇怪的问题。
目前主要window:
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));
}
}
我正在使用的数据 class 将 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));
}
}
主要window 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>
当前输出是一个空的树视图,其中没有任何内容,即使数据绑定对象充满了信息。
我意识到这可能是一个愚蠢的错误,但我似乎真的找不到错误,求助?
你能在实现 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 数据绑定只能(我忘记了)属性,而不是字段,即使字段是 public,将所有数据绑定字段更改为实际属性可以解决这个问题。