![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 |
| | #2 (permalink) |
| Guest | 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 |
| | #3 (permalink) |
| Guest | 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 |
| | #4 (permalink) |
| Guest | 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 |
| | #5 (permalink) |
| Guest | 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 > > > |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: "Sort By Date" in 'Sent Items' Missing? | _mikebs | Live Mail | 0 | 12-03-2007 10:04 PM |
| "Sort By Date" in 'Sent Items' Missing? | Ramie416 | Live Mail | 3 | 11-20-2007 08:24 PM |
| Textbox value to filter databound ListBox items? | Olav | Avalon | 2 | 09-23-2006 12:55 PM |
| How can I scroll the items at listbox or listview by using storybo | Jeffrey_Wu | Avalon | 0 | 06-26-2006 07:34 AM |
| How to navigate through ListBox items via a style/template? | Pascal Bourque | Avalon | 0 | 01-31-2006 04:47 PM |