Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

How to sort items of a ListBox bound to an XmlDataProvider?

Closed Thread
 
Thread Tools Display Modes
Old 06-08-2006   #1 (permalink)
Josh Smith
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
Old 06-10-2006   #2 (permalink)
viliescu
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

Old 06-10-2006   #3 (permalink)
Josh Smith
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

Old 06-10-2006   #4 (permalink)
Vipin
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



Old 06-11-2006   #5 (permalink)
Josh Smith
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

>
>
>

Closed Thread

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








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50