Search code examples
wpfmvvmdatagridshort

Order elements in view or in view model?


I am using the MVVM pattern and I would like to sort the elements on a datagrid. In my view model I have an ObservableCollection with the element for the datagrid.

In the datagrid I can sort element cliking in the column name, so that is nice, and for multiple columns. I like this feature.

However, I would like to do some more specific ordering, for example show first the element which the ColumnB has "valueC" and later the rest of the elements, and when I sort for a column name, order for that column but show first the elements with "valueC" and later the rest of the elements sorted by the column that I clicked.

To do that, I can sort the elements in the ObservableCollection of the view model, but I think that this functionality is more something that it would be done in view, plus because I would like to sort by columns, so I would have to say to the view the column that is clicked.

However, I don't know if it is posible to do this kind of complex sorts in the view, perhaps with some converter or with any way.

Thank so much.


Solution

  • Every logic has to be done in the ViewModel. View must not know anything about the logic behind displayed datas.

    In a simple case you could order your items in a view but in a realistic case, ordering will not affect only visible items but it affects also items not shown because of the paging.