Sorting an ItemCollection - C# 3.5 - WPF

E

escher4096

I am attempting to sort the ItemsCollection of a ListView in C# / WPF.
I have it all hooked up and for the base case it is working fine. Base
case assumes binding to a single datatable in a dataset:

/// there is a bunch of other code before this but it all just
sets up for this
/// one method which actually does the sorting

/// <summary>
/// Does the column sorting
/// </summary>
/// <param name="columnBindToPropertyToSortOn">Which property
should we sort on</param>
/// <param name="direction">What direction should we sort on</
param>
protected virtual void Sort(string
columnBindToPropertyToSortOn, ListSortDirection direction)
{
if (null != columnBindToPropertyToSortOn)
{
Items.SortDescriptions.Clear();
SortDescription sd = new
SortDescription(columnBindToPropertyToSortOn, direction);
Items.SortDescriptions.Add(sd);
Items.Refresh();
}
}

This works perfectly if we want to sort by exactly what is in the
table. But I have 2 cases where this doesn't make sense.

Case 1: Where we are storing GUIDs in the bound table to our look up
table and displaying the value through a converter or combo box (where
the combo box item source is bound to the lookup table and selected
value is bound to the GUID in our current table). Sorting by a GUID,
while fun, rarely results in a nice alphabetic sorting. I need to sort
by the value we get from the converter/display value in the combobox,
not the GUID we are storing in the view. Since we are allowing the
user to edit the values in the list view (through the combobox) we
can't just do the conversion from GUID to pretty name in the SQL
before displaying the results.

Case 2: Binding through related data. If we have 4 columns in our list
and:
column 1 is bound to table 1 / column 1
column 2 is bound to table 1 / column 2
column 3 is bound to table 1 / some relation to table 2 / column
1
column 4 is bound to table 1 / some relation to table 2 / some
relation in table 3 / column 2
then the display is fine and sorting works for the first 2 columns
but the sorting borks on column 3 and 4 because the column isn't in
the current table.

I have looked at building a custom 'SortDescription' class but it is a
struct (which means no inheritance) and does have an interface. I have
not figured out where in the ItemsCollection the sort actually happens
(thinking that maybe I could write an extension method or inherit from
it and override some method).

I have attempted to sort the items in ListView.Items manually but of
course you can't do that because the items belong to the underlying
view to which we don't have access. We can't pass in an ICompare or
anything like that. The only way to sort the collection that I can
find (SortDescription) does not allow you to do anything funky.

Any help would be appreciated.

Thanks

-Cam
 
C

CyberMan_(MCDST)_nuub

Im no expert, but can you not populate a dictionary with your Idenity and
description fields

setup a delegate Sort and and use a predicate to sort the dictionary contents

Then return the list of identities and use this order to load from your table?

Hope this helps...
 
E

escher4096

I don't think that is fessible. While we could put the data into a
dictionary and sort it there we don't want to hit the database every
time the user sorts a grid (this application has stacks of grids) so
going back to the database table for every sort is out. WPF has enough
performance issues without hitting the database that many times. We
can't use the dictionary as our display data since we are using two
way data binding.

We can't use delegate sort or a predicate to sort a WPF listview since
there isn't an interface to support this and there isn't a method to
override to stick it in there.

Thanks for the suggestion though.

-Cam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top