c# - 以编程方式将 xml 文档绑定(bind)到 WPF 中的 TreeView

标签 c# wpf xml data-binding treeview

我一直在谷歌上进行一些搜索,并在此处阅读一些类似的问题,但尚未找到我的问题的答案。我正在将 xml 文件绑定(bind)到 WPF 中的 TreeView 控件。使用this文章 我可以轻松地使用 xml 文件设置 2 路数据绑定(bind)。

但是,我想在附加 xml 文档之前对其进行排序。我正在建模一个任务组织器,其中任务包含开始日期和截止日期,我想按待定截止日期对节点进行排序,以便最紧急的任务首先出现。我对 Linq to XML 有一些经验,但不确定如何解决绑定(bind)问题。有什么想法吗?

因此,经过更多阅读后,这里是我的伪代码:

  • 从本地 XML 文件创建 XDocument
  • 根据任务截止日期对 XDocument 进行排序
  • 从新排序的 XDocument 创建新的 XmlDataProvider
  • 将其绑定(bind)到 TreeView

谁能帮我解决这个问题吗?

最佳答案

下面是如何实现 XML 到 TreeView 绑定(bind)的基本示例。 XmlDataProvider 允许您加载 XML 文档并绑定(bind)到它。 HierarchicalDataTemplate 允许您定义带有子项的 TreeView 模板。必须使用XPath而不是Path进行绑定(bind),@符号前缀表示绑定(bind)到属性。

<Window x:Class="Testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="people" XPath="People" />

        <HierarchicalDataTemplate x:Key="colorsTemplate">
            <TextBox Text="{Binding XPath=@Name, Mode=TwoWay}" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="rootTemplate" ItemsSource="{Binding XPath=FavoriteColors/Color}" ItemTemplate="{StaticResource colorsTemplate}">
            <StackPanel Orientation="Horizontal">
                <TextBox Text="{Binding XPath=@FirstName, Mode=TwoWay}" />
                <TextBlock Text=" " />
                <TextBox Text="{Binding XPath=@LastName, Mode=TwoWay}" />
                <TextBlock Text=" (Age: " />
                <TextBox Text="{Binding XPath=@Age, Mode=TwoWay}" />
                <TextBlock Text=")" />
            </StackPanel>
        </HierarchicalDataTemplate>

    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding Source={StaticResource people}, XPath=Person}" ItemTemplate="{StaticResource rootTemplate}" Grid.ColumnSpan="2" />
    </Grid>
</Window>

使用以下 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<People>
  <Person FirstName="Ringo" LastName="Starr" Age="72">
    <FavoriteColors />
  </Person>
  <Person FirstName="George" LastName="Harrison" Age="52">
    <FavoriteColors>
      <Color Name="Orange" />
      <Color Name="Green" />
      <Color Name="Purple" />
    </FavoriteColors>
  </Person>
  <Person FirstName="Paul" LastName="McCartney" Age="42">
    <FavoriteColors>
      <Color Name="White" />
    </FavoriteColors>
  </Person>
  <Person FirstName="John" LastName="Lennon" Age="33">
    <FavoriteColors>
      <Color Name="Red" />
      <Color Name="Green" />
    </FavoriteColors>
  </Person>
</People>

以及以下隐藏代码:

XmlDataProvider people;

public MainWindow()
{
    InitializeComponent();

    people = FindResource("people") as XmlDataProvider;

    var xmlDocument = new XmlDocument();

    xmlDocument.Load("People.xml");

    people.Document = xmlDocument;
}

正如您所看到的,我正在代码中加载 XML 文档,因此您可以将其加载到 XDocument 或 XmlDocument 类中并按照您想要的方式对其进行排序。然后您应该能够在某个时候将其保存回文件。

编辑:

下面是运行时加载和保存的示例:

private void Load_Click(object sender, RoutedEventArgs e)
{
    var xmlDocument = new XmlDocument();

    xmlDocument.Load("People.xml");

    people.Document = xmlDocument;
}

private void Save_Click(object sender, RoutedEventArgs e)
{
    XmlDocument xml = people.Document;

    if (xml != null)
    {
        Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();

        if ((bool)sfd.ShowDialog(this))
        {
            xml.Save(sfd.FileName);
        }
    }
}

关于c# - 以编程方式将 xml 文档绑定(bind)到 WPF 中的 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10953835/

相关文章:

c# - GetSchema 方法可以异步工作吗?

c# - NHibernate - KeyNotFoundException : The given key was not present in the dictionary

c# - 无需显式重新创建的 WPF 刷新 CollectionView(Refresh() 方法调用)

sql - 获取 SQL Server 中特定的 XML 子节点

java - 无法使用java将文件从一个xml写入另一个xml

java - 在java中用公钥加密,在C#中用私钥RSA解密

c# - 复杂的 LINQ 查询与复杂的 for 循环

c# - WPF 相当于 Winform 的 KeyEventArgs - 使用特定键调用 KeyDown 事件

c# - Send Message 和 Post Message 之间有什么区别,它们与 C#、WPF 和纯 Windows 编程有什么关系?

html - 有没有办法使用 XPath 选择特定的父级?