Currently, the following IParents implement the ICanSort interface:
- ListBox
- Grid
- DataGrid
- StackPanel
- WrapPanel
- TreeView
- DockPanel

The TWO most important methods in this interface are:

void BeforeSortCollection<T>(object chain, object args, int row, int column, FasterWPF.CommonExt.SortOrder sortOrder, CommonExt.SortControlType sortControlType)
where T : FrameworkElement;

void BeforeSortCollection1Then2<T>(object chain, object args, int row, int column, int row2, int column2, FasterWPF.CommonExt.SortOrder sortOrder, FasterWPF.CommonExt.SortOrder sortOrder2, CommonExt.SortControlType sortControlType)
where T : FrameworkElement;

These methods are used internally within the more convenient and friendlier-named public static extension methods:
- SortByText<T>
- SortByText1AndThenBy2<T> methods.

The first sorts by an element at a single row-column (x-y coordinate) position within a Composite. The second sorts step-wise first by one row-column position and then by a second row-column position.

Each element at a row-column position to be sorted by should be one of three Sort Control Types:
- TextBlock
- TextBox
- Label

Only one Sort Control Type may be used in a given sort, even when sorting step-wise. So the elements must be all TextBlocks, or all Labels, or all TextBoxes.

The text within these three types of controls may be of different data types. For instance, if a number is used in a TextBlock, the sort code may be requested to cast this string value to an integer, if desired. This is achieved by passing an args parameter that implements the interface ISortArgs.

ISortArgs requires two properties--a SortDataType and SortDataType2--of the enum type SortDataType:
public enum SortDataType
       String = 0,
       DateTime = 2,
       Decimal = 3,
       Integer = 4
SortDataType and SortDataType2 may be of different data types, e.g. one could be String and the other Integer, if you are performing a step-wise sort by first one and then the other.

Differences Between SortByText<T> and SortByText1AndThenBy2<T>

Both methods will handle setting default values if the element at the given row-column exists but is not one of the three supported sort control types: TextBlock, TextBox, or Label. For instance, if I am sorting a List Box by position 0,0 and every Composite contains a TextBlock there except for one which has a Rectangle in this position, then, for sorting purposes, the Rectangle will be treated as a default value of empty string.

The default value used will depend on the data type being sorted on, namely:
- 1/1/1900 for DateTime
- 0.00 for Decimal
- 0 for Integer
- "" for String

The primary way in which SortByText<T> and SortByText1AndThenBy2<T> differ (besides the extra step-wise sorting in the latter) is in how they handle the case when there is no element at the given row-column at all!

In the case of SortByText<T>, any Composites that do not have an element at the given row-column position will merely be tacked on to the end when re-adding the Composites to the IParent.

In the case of SortByText1AndThenBy2<T>, any Composites that do not have an element at the given row-column position will be treated in the same manner as the mis-matching sort control types; in other words, for sorting purposes an appropriate default value will be provided if no element exists at the row-column position.

The reason for this divergence is to allow support for both preferences. If you prefer missing elements to be treated with default values, use the one method. If you prefer missing elements to cause tacking on of Composites at the end, use the other method.

How Sorting Works

Sorting works by first removing all Composites from the IParent, sorting them via LINQ's orderby keyword (either ascending or descending), and then re-adding the Composites in the sorted order.

For Grid and DockPanel, where a Row-Column or Dock parameter must be provided, the sort methods first record the parameters that were originally used. For instance, if the original Composites were added Docked at Left, Right, and Bottom, then the sorted Composites will be re-added with the first Composite docked Left, the second Composite docked Right, and the third Composite docked at Bottom.

This is done to allow for a sense of consistency.

Also, it is worth noting that the TreeView only sorts based on the top level or parent nodes. It does not allow deep-diving into the child-level of nodes, excluding them for sorting purposes. Child-level nodes merely remain attached to their parents and move with their parents as these are moved/sorted.

Basic Example of Sorting

Here is an example of the step-wise sorting by textblock 1 and then by textblock 2 within each composite. Such code might look like this:
StackPanelSort stackSrt = new StackPanelSort(
CommonExt.SortDataType.String, CommonExt.SortDataType.Integer);

myStackPanel.SortByText1AndThenBy2<StackPanel>(stackSrt, 0,0, 1,0, 
SortOrder.Desc, SortOrder.Desc, 

Last edited Mar 20, 2014 at 6:01 AM by stagathome0069, version 23


No comments yet.