![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | How to sort items of a ListBox bound to an XmlDataProvider? Hello all, I am binding a ListBox to an XmlDataProvider. The data is shaped something like this: <Contacts> <Contact> <Name> <First>John</First> <Middle>A</Middle> <Last>Doe</Last> </Name> <OtherData>...</OtherData> </Contact> <!-- more Contact elements... --> </Contacts> The ListBox's ItemTemplate displays each Contact as such: [Last], [First] [Middle] The template is: <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding XPath=./Name/Last}" /> <TextBlock Text=", " /> <TextBlock Text="{Binding XPath=./Name/First}" /> <TextBlock Text=" " /> <TextBlock Text="{Binding XPath=./Name/Middle}" /> </StackPanel> </DataTemplate> I want to sort the ListBoxItems based on their display text (the formatted name), but I cannot find a way to do this. The ListBox.Items can be sorted via SortDescription objects (which are added to the SortDescriptions property). SortDescription objects use PropertyName and ListSortDirection settings to describe how to sort the list. So here's the problem...What property name should I assign to the PropertyName setting of a SortDescription object? I tried several properties, including Content, but none of them worked. I then noticed that ListBox.Items exposes a Comparer property, but it is read-only, so that was not helpful either. I found samples online that show how to sort ListBox items without using SortDescriptions, but in all of those samples the data source is a collection, not an XmlDataProvider (hence, XmlDocument). Since XmlDocument does not support sorting, I can't go that route either. Can someone point me in the right direction, please? This just seems like an impossible task, yet it is a very reasonable and common thing to do. Thanks in advance, Josh Smith |
My System Specs![]() |
| | #2 (permalink) |
| | RE: How to sort items of a ListBox bound to an XmlDataProvider? ListCollectionView has a property CustomSort of type IComparer. Implement your own IComparer, the objects passed to the Compare method will be of XmlElement type. -- Valentin Iliescu [MVP - Client Application Development] "Josh Smith" wrote: > Hello all, > > I am binding a ListBox to an XmlDataProvider. The data is shaped something > like this: > > <Contacts> > <Contact> > <Name> > <First>John</First> > <Middle>A</Middle> > <Last>Doe</Last> > </Name> > <OtherData>...</OtherData> > </Contact> > <!-- more Contact elements... --> > </Contacts> > > The ListBox's ItemTemplate displays each Contact as such: > > [Last], [First] [Middle] > > The template is: > > <DataTemplate> > <StackPanel Orientation="Horizontal"> > <TextBlock Text="{Binding XPath=./Name/Last}" /> > <TextBlock Text=", " /> > <TextBlock Text="{Binding XPath=./Name/First}" /> > <TextBlock Text=" " /> > <TextBlock Text="{Binding XPath=./Name/Middle}" /> > </StackPanel> > </DataTemplate> > > I want to sort the ListBoxItems based on their display text (the formatted > name), but I cannot find a way to do this. The ListBox.Items can be sorted > via SortDescription objects (which are added to the SortDescriptions > property). SortDescription objects use PropertyName and ListSortDirection > settings to describe how to sort the list. > > So here's the problem...What property name should I assign to the > PropertyName setting of a SortDescription object? I tried several > properties, including Content, but none of them worked. I then noticed that > ListBox.Items exposes a Comparer property, but it is read-only, so that was > not helpful either. > > I found samples online that show how to sort ListBox items without using > SortDescriptions, but in all of those samples the data source is a > collection, not an XmlDataProvider (hence, XmlDocument). Since XmlDocument > does not support sorting, I can't go that route either. > > Can someone point me in the right direction, please? This just seems like > an impossible task, yet it is a very reasonable and common thing to do. > > Thanks in advance, > Josh Smith |
My System Specs![]() |
| | #3 (permalink) |
| | RE: How to sort items of a ListBox bound to an XmlDataProvider? That did the trick. Thanks a lot! For anyone facing the same problem I did, here's the code I used to solve it: void OnWindowLoaded( object sender, RoutedEventArgs e ) { XmlDataProvider dataProv = this.FindResource( "contactsData" ) as XmlDataProvider; ListCollectionView view = CollectionViewSource.GetDefaultView( dataProv.Data ) as ListCollectionView; view.CustomSort = new ContactElementComparer(); } Josh "viliescu" wrote: > ListCollectionView has a property CustomSort of type IComparer. > > Implement your own IComparer, the objects passed to the Compare method will > be of XmlElement type. > -- > Valentin Iliescu [MVP - Client Application Development] > > > "Josh Smith" wrote: > > > Hello all, > > > > I am binding a ListBox to an XmlDataProvider. The data is shaped something > > like this: > > > > <Contacts> > > <Contact> > > <Name> > > <First>John</First> > > <Middle>A</Middle> > > <Last>Doe</Last> > > </Name> > > <OtherData>...</OtherData> > > </Contact> > > <!-- more Contact elements... --> > > </Contacts> > > > > The ListBox's ItemTemplate displays each Contact as such: > > > > [Last], [First] [Middle] > > > > The template is: > > > > <DataTemplate> > > <StackPanel Orientation="Horizontal"> > > <TextBlock Text="{Binding XPath=./Name/Last}" /> > > <TextBlock Text=", " /> > > <TextBlock Text="{Binding XPath=./Name/First}" /> > > <TextBlock Text=" " /> > > <TextBlock Text="{Binding XPath=./Name/Middle}" /> > > </StackPanel> > > </DataTemplate> > > > > I want to sort the ListBoxItems based on their display text (the formatted > > name), but I cannot find a way to do this. The ListBox.Items can be sorted > > via SortDescription objects (which are added to the SortDescriptions > > property). SortDescription objects use PropertyName and ListSortDirection > > settings to describe how to sort the list. > > > > So here's the problem...What property name should I assign to the > > PropertyName setting of a SortDescription object? I tried several > > properties, including Content, but none of them worked. I then noticed that > > ListBox.Items exposes a Comparer property, but it is read-only, so that was > > not helpful either. > > > > I found samples online that show how to sort ListBox items without using > > SortDescriptions, but in all of those samples the data source is a > > collection, not an XmlDataProvider (hence, XmlDocument). Since XmlDocument > > does not support sorting, I can't go that route either. > > > > Can someone point me in the right direction, please? This just seems like > > an impossible task, yet it is a very reasonable and common thing to do. > > > > Thanks in advance, > > Josh Smith |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to sort items of a ListBox bound to an XmlDataProvider? Just out of curiosity, are you embedding your xml file as a resource in the assembly. May want to ship as a standalone file along with the assembly. -- Vipin Aravind http://www.explorewindows.com "Josh Smith" <JoshSmith@discussions.microsoft.com> wrote in message news:2BC4AAEB-3371-4273-BCD6-2D60FE3F2C07@microsoft.com... > That did the trick. Thanks a lot! > > For anyone facing the same problem I did, here's the code I used to solve > it: > > void OnWindowLoaded( object sender, RoutedEventArgs e ) > { > XmlDataProvider dataProv = this.FindResource( "contactsData" ) as > XmlDataProvider; > ListCollectionView view = CollectionViewSource.GetDefaultView( > dataProv.Data ) as ListCollectionView; > view.CustomSort = new ContactElementComparer(); > } > > Josh > > "viliescu" wrote: > >> ListCollectionView has a property CustomSort of type IComparer. >> >> Implement your own IComparer, the objects passed to the Compare method >> will >> be of XmlElement type. >> -- >> Valentin Iliescu [MVP - Client Application Development] >> >> >> "Josh Smith" wrote: >> >> > Hello all, >> > >> > I am binding a ListBox to an XmlDataProvider. The data is shaped >> > something >> > like this: >> > >> > <Contacts> >> > <Contact> >> > <Name> >> > <First>John</First> >> > <Middle>A</Middle> >> > <Last>Doe</Last> >> > </Name> >> > <OtherData>...</OtherData> >> > </Contact> >> > <!-- more Contact elements... --> >> > </Contacts> >> > >> > The ListBox's ItemTemplate displays each Contact as such: >> > >> > [Last], [First] [Middle] >> > >> > The template is: >> > >> > <DataTemplate> >> > <StackPanel Orientation="Horizontal"> >> > <TextBlock Text="{Binding XPath=./Name/Last}" /> >> > <TextBlock Text=", " /> >> > <TextBlock Text="{Binding XPath=./Name/First}" /> >> > <TextBlock Text=" " /> >> > <TextBlock Text="{Binding XPath=./Name/Middle}" /> >> > </StackPanel> >> > </DataTemplate> >> > >> > I want to sort the ListBoxItems based on their display text (the >> > formatted >> > name), but I cannot find a way to do this. The ListBox.Items can be >> > sorted >> > via SortDescription objects (which are added to the SortDescriptions >> > property). SortDescription objects use PropertyName and >> > ListSortDirection >> > settings to describe how to sort the list. >> > >> > So here's the problem...What property name should I assign to the >> > PropertyName setting of a SortDescription object? I tried several >> > properties, including Content, but none of them worked. I then noticed >> > that >> > ListBox.Items exposes a Comparer property, but it is read-only, so that >> > was >> > not helpful either. >> > >> > I found samples online that show how to sort ListBox items without >> > using >> > SortDescriptions, but in all of those samples the data source is a >> > collection, not an XmlDataProvider (hence, XmlDocument). Since >> > XmlDocument >> > does not support sorting, I can't go that route either. >> > >> > Can someone point me in the right direction, please? This just seems >> > like >> > an impossible task, yet it is a very reasonable and common thing to do. >> > >> > Thanks in advance, >> > Josh Smith |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to sort items of a ListBox bound to an XmlDataProvider? No, the xml file is not embedded in the assembly. The app I'm building allows the user to add/update/delete items. It would be handy, however, if there were a way to swap out an assembly's old embedded xml file with a modified version. I assume this couldn't work for strongly named assemblies because the runtime hash verification would fail, but for unsigned assemblies, it would be great. But I digress... Josh "Vipin" wrote: > Just out of curiosity, are you embedding your xml file as a resource > in the assembly. May want to ship as a standalone file along with the > assembly. > > -- > Vipin Aravind > http://www.explorewindows.com > > > "Josh Smith" <JoshSmith@discussions.microsoft.com> wrote in message > news:2BC4AAEB-3371-4273-BCD6-2D60FE3F2C07@microsoft.com... > > That did the trick. Thanks a lot! > > > > For anyone facing the same problem I did, here's the code I used to solve > > it: > > > > void OnWindowLoaded( object sender, RoutedEventArgs e ) > > { > > XmlDataProvider dataProv = this.FindResource( "contactsData" ) as > > XmlDataProvider; > > ListCollectionView view = CollectionViewSource.GetDefaultView( > > dataProv.Data ) as ListCollectionView; > > view.CustomSort = new ContactElementComparer(); > > } > > > > Josh > > > > "viliescu" wrote: > > > >> ListCollectionView has a property CustomSort of type IComparer. > >> > >> Implement your own IComparer, the objects passed to the Compare method > >> will > >> be of XmlElement type. > >> -- > >> Valentin Iliescu [MVP - Client Application Development] > >> > >> > >> "Josh Smith" wrote: > >> > >> > Hello all, > >> > > >> > I am binding a ListBox to an XmlDataProvider. The data is shaped > >> > something > >> > like this: > >> > > >> > <Contacts> > >> > <Contact> > >> > <Name> > >> > <First>John</First> > >> > <Middle>A</Middle> > >> > <Last>Doe</Last> > >> > </Name> > >> > <OtherData>...</OtherData> > >> > </Contact> > >> > <!-- more Contact elements... --> > >> > </Contacts> > >> > > >> > The ListBox's ItemTemplate displays each Contact as such: > >> > > >> > [Last], [First] [Middle] > >> > > >> > The template is: > >> > > >> > <DataTemplate> > >> > <StackPanel Orientation="Horizontal"> > >> > <TextBlock Text="{Binding XPath=./Name/Last}" /> > >> > <TextBlock Text=", " /> > >> > <TextBlock Text="{Binding XPath=./Name/First}" /> > >> > <TextBlock Text=" " /> > >> > <TextBlock Text="{Binding XPath=./Name/Middle}" /> > >> > </StackPanel> > >> > </DataTemplate> > >> > > >> > I want to sort the ListBoxItems based on their display text (the > >> > formatted > >> > name), but I cannot find a way to do this. The ListBox.Items can be > >> > sorted > >> > via SortDescription objects (which are added to the SortDescriptions > >> > property). SortDescription objects use PropertyName and > >> > ListSortDirection > >> > settings to describe how to sort the list. > >> > > >> > So here's the problem...What property name should I assign to the > >> > PropertyName setting of a SortDescription object? I tried several > >> > properties, including Content, but none of them worked. I then noticed > >> > that > >> > ListBox.Items exposes a Comparer property, but it is read-only, so that > >> > was > >> > not helpful either. > >> > > >> > I found samples online that show how to sort ListBox items without > >> > using > >> > SortDescriptions, but in all of those samples the data source is a > >> > collection, not an XmlDataProvider (hence, XmlDocument). Since > >> > XmlDocument > >> > does not support sorting, I can't go that route either. > >> > > >> > Can someone point me in the right direction, please? This just seems > >> > like > >> > an impossible task, yet it is a very reasonable and common thing to do. > >> > > >> > Thanks in advance, > >> > Josh Smith > > > |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Re: "Sort By Date" in 'Sent Items' Missing? | Live Mail | |||
| "Sort By Date" in 'Sent Items' Missing? | Live Mail | |||