如何使用XmlDataProvider对传入的xml数据进行排序? [英] How do I sort incoming xml data using XmlDataProvider?

查看:61
本文介绍了如何使用XmlDataProvider对传入的xml数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Grid.DataContext>
            <XmlDataProvider x:Name="MyData" Source="MyData.xml" XPath="Contacts/Contact" />
</Grid.DataContext>



我最难找到一种方法来对来自XmlDataProvider的传入数据进行排序。字段< company>是一个ComboBox,需要从A到Z排序。有人可以帮忙吗?


I am having the hardest time finding a way to sort the incoming data from XmlDataProvider. The field <company> is going to a ComboBox and needs to be sorted from A to Z. Can someone please help?

推荐答案

有几个消息来源提到使用 CollectionViewSource https: //social.msdn.microsoft.com/Forums/silverlight/en-US/95031d3f-1b7d-45b9-88d3-07a07c2ea90d/data-sorting-with-xmldataprovider?forum=silverlightcontrols [ ^ ])

Several sources mention using CollectionViewSource (https://social.msdn.microsoft.com/Forums/silverlight/en-US/95031d3f-1b7d-45b9-88d3-07a07c2ea90d/data-sorting-with-xmldataprovider?forum=silverlightcontrols[^])
<window>
   <Window.Resources>
       <XmlDataProvider x:Key="xmlData" Source="MyData.xml" XPath="CustDB/Customer"/>
   </Window.Resources>

   <Grid.DataContext>
     <CollectionViewSource x:Name="dataCvs" Source="{Binding Source={StaticResource xmlData}}" />
   </Grid.DataContext>

</window>



在后面的代码中添加以下内容(某些Loaded或Click事件):


To the code behind you add somewhere (some Loaded or Click event) the following:

dataCvs.SortDescriptions.Clear()
dataCvs.SortDescriptions.Add(
     New SortDescription With {.PropertyName = "Name"})
     'PropertyName should be the tag in the XML file



但是,它更简单,更推荐使用Linq。就像你以前尝试的那样:):将文本框绑定到组合填充了LINQ to XML数据的框 [ ^ ]


这篇关于如何使用XmlDataProvider对传入的xml数据进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆