Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

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

 
 
Old 06-08-2006   #1 (permalink)
Josh Smith


 
 

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 SpecsSystem Spec
Old 06-10-2006   #2 (permalink)
viliescu


 
 

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


 
 

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


 
 

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


 
 

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 SpecsSystem Spec
 

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


Vista Forums 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 Ltd

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