This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm looking to implement custom sorting on a DataGrid. Namely, I have a column that contains Id values for the Path to which an element belongs, and I'd like to be able to sort by that column, but not by Id. The display value in the column is not this Id, but rather a name for the path, fetched through a converter that gets the description string from the ViewModel--- So when I sort, it sorts by the Id, not by the name of the path, which is confusing to the end user. If the DataGrid implemented something like the SortDescriptions collection that the property grid uses, I have a pretty clear idea how to do that. However, currently the only functionality I can find on the DataGrid is the ability to change the sort direction. It would be ideal if I could simply designate a "SortByValue" on the column that was the property by which to sort. |
|
|
Hello Thanks for the feedback. Just to clarify, when you mention the "SortByValue" property, would you expect this to be a property that allows you to specify which property on the row data objects to sort by? Would you want this to be a string property or a Binding property? Cheers -Jason |
|
|
Yes, I would expect the "SortByValue" to be which property (of type string) of the row data objects to sort by when the column header is clicked. An example:
Where "EntityConverter" is a MultiValueConverter that gets the .Name or .Description property of an Entity from a list of Entities (in this case, DataContext.Paths). |
|
|
Thanks for the clarification. We will look into this shortly. -Jason |
|
|
Okay, this will be available in the next nightly build. We have added a new SortComparer property to the DataGridColumn, of type Note that the comparer compares the underlying objects, not the column values. For example, suppose you had a Name column and you wanted its sort behaviour to be by length instead of alphabetical (a silly example, I know). Then your comparer would need to be along the lines of:
This is necessary to support using multiple object properties in the sort as per your example. Example of usage:
Let us know if you need any further info or run into any problems. |
|
|
Is there any way we could get a binding? Bindings are more generic. Binding paths also support "drill-down" like Property.Property or Property[Index].Property for reference type properties. It seems that sorting and grouping in the DataGrid is based on the DataGridColumn.PropertyName. Could another property be added to support binding (i.e. "PropertyBinding" or "Binding") without breaking existing code? This would help me avoid writing my own display template with just a TextBlock and a binding on the Text property, as well as a custom SortComparer (see below).
Thanks for all you guys do! |
|
|
Hello Jared The next nightly build will include a DisplayMemberBinding property on DataGridColumn which lets you set bindings as you have described. This feature is currently only available when the data grid is not automatically populating the columns - let us know if you need support for this. NOTE: if you need a writeable column, make sure to set Mode=TwoWay on the binding. I have tested this feature against a lot of common use cases, but as this feature is very new, let us know if you come across any scenarios that don't seem to behave as expected. Jason Fauchelle |
|
|
Thanks Jason! After my initial testing, this seems to work very well! Thank you for the quick turn around time! |
|