2012-06-08 53 views
2

我一直在google上做一些搜索,并在这里阅读了一些类似的问题,但还没有找到我的问题的答案。我将一个xml文件绑定到WPF中的treeview控件。使用this文章,我可以轻松地使用我的xml文件设置双向数据绑定。以编程方式将WPF中的xml文档绑定到treeview

但是,我想在我附加它之前对我的xml文档应用排序。我正在为一个任务组织者建模,其中任务包含开始日期和截止日期,我希望通过待定截止日期排序节点,因此最紧急的任务首先出现。我有Linq to XML的一些经验,但不知道如何处理绑定问题。有什么想法吗?

所以一些更多的阅读后,这里是我的伪代码:

  • 从我的本地XML文件中创建一个XDocument
  • 排序的XDocument基础上,由于任务的日期
  • 创建从一个新的XmlDataProvider新排序的XDocument
  • 将其绑定到TreeView

谁能帮我冲洗了这一点?

+0

你的问题不符合问题的标题。这是关于排序一个XML文件或绑定到一个TreeView?看来你的问题只是关于XML,而不是关于WPF。一旦它被排序后,你想要绑定它,并从你说的绑定已经工作。 –

+0

对不起,我想这需要澄清。我可以使用linq to xml轻松地对xml文档进行排序,但是一旦它被排序并存储在内存中,我该如何将它绑定到treeview? –

回答

3

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

<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 [email protected], Mode=TwoWay}" /> 
     </HierarchicalDataTemplate> 

     <HierarchicalDataTemplate x:Key="rootTemplate" ItemsSource="{Binding XPath=FavoriteColors/Color}" ItemTemplate="{StaticResource colorsTemplate}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBox Text="{Binding [email protected], Mode=TwoWay}" /> 
       <TextBlock Text=" " /> 
       <TextBox Text="{Binding [email protected], Mode=TwoWay}" /> 
       <TextBlock Text=" (Age: " /> 
       <TextBox Text="{Binding [email protected], 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); 
     } 
    } 
} 
+0

这非常酷,但我想知道是否有可能使它更通用。换句话说,你将如何为* any * xml文件做这项工作?如果属性(例如FirstName)未知,并且每次都会有所不同,您将如何处理'{Binding XPath = @ FirstName,Mode = TwoWay}'? (如果你愿意,我会把它当成自己的问题) –