Click here to Skip to main content
15,941,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<Grid.DataContext>
            <XmlDataProvider x:Name="MyData" Source="MyData.xml" XPath="Contacts/Contact" />
</Grid.DataContext>

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?
Posted

1 solution

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[^])
XML
<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>

To the code behind you add somewhere (some Loaded or Click event) the following:
VB
dataCvs.SortDescriptions.Clear()
dataCvs.SortDescriptions.Add(
     New SortDescription With {.PropertyName = "Name"})
     'PropertyName should be the tag in the XML file

However, it's simpler and more recommended to use Linq. Like you previously tried :) : Binding Text Box to Combo Box populated with LINQ to XML data[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900