开发者

Datagrid cannot sort on some fields - object, object subfield, base type

开发者 https://www.devze.com 2022-12-12 20:21 出处:网络
When using the Silverlight DataGrid with an IList derived data source it is possible to click a column to sort by it, assuming the CanUserSortColumns/CanUserSort properties are not false.

When using the Silverlight DataGrid with an IList derived data source it is possible to click a column to sort by it, assuming the CanUserSortColumns/CanUserSort properties are not false.

However I'm finding some edge cases cannot be sorted by - the values are shown, but clicking the column has no effect.

The first edge cases would seem to be the very simple List<string>, which shows the values, but does not allow sorting. This doesn't make much sense as string implime开发者_Go百科nts IComparable.

The second edge case is much more complex, and may need a work around. The second case involves fields and sub-fields of type object.

public class ContainerItem
{       
   public object ChildValue {get; set;}

   // Other properties
}

The value of ChildValue can be displayed in the grid, and if the type assigned to ChildValue has properties we can also bind to them (ChildValue.Property), but we cannot sort the column.

It's obvious that the lack of a defined type is an issue here - there would obviously be problems if the objects where of different, incompatible types. In my specific situation I'm stuck with this as the ChildValue is assigned dynamically, as are the column binding definitions.

It seems obvious that I need a work around - I do have control over the ContainerItem class definition, and the bindings to properties of ChildValue are always OneWay (only the fixed properties of ContainerItem use two way binding).

Could I perhaps use the SortMemberPath and bind it to some array property of ContainerItem, where each element of the array is a ContainerItemComparer object filled with the binding information of the column so that it can pull the values from the two ChildValue properties, compare them, and return the result without the DataGrid being aware that the properties are naked objects?


I think the first edge case is pretty artificial. Why would you bind a list of strings to a datagrid? It probably doesn't work because there is no property name for it to use as a sort path.

For the question of sorting dynamically, you would probably be better off implementing ICollectionView in your collection. Then you can control the sorting manually however you want to handle it without trying to force it to work from the built in sorting.

0

精彩评论

暂无评论...
验证码 换一张
取 消