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