3

I'm coming from a background and trying to get ahold of MVVM and XAML. Right now I'm wondering how I can take advantage from a data-shaping control (either native or provided by a third party) while maintaining separation of concerns intact.

An example: I have a typical third party grid that comes with data grouping, sorting and filtering functionalities. I want the end user to be able to use these features so that he can get a custom set of data on which he can perform actions. These actions are exposed by the view model. Of course it knows about the record collection but it has absolutely no clue about how it is currently displayed in the UI.

Given that, how can the view model know on which data actions should be performed, without knowing about the control in which it is displayed?

Right now the only way I can think of is exposing an additional property in the ViewModel (something like CurrentUIData) and handling every single data-shaping events within the View so that the property is always representative of the UI state. This, however, feels kind of wrong to me.

Is this how I should proceed, or am I missing something here?

  • Unless all of your other controls are ones you've created, how is this any different than a grid control provided by MS? – JeffO May 8 '14 at 15:58
  • @JeffO I'm evoking third parties because I don't want somebody to propose a workaround that's specific to a particular control. I'm trying to look at the wider picture and become aware of common rules of thumb, should there be any. – Crono May 8 '14 at 16:58
  • If you need to know that the data is filtered on the ViewModel to action it, you need to implement the filter on the ViewModel. Same goes for sorting, and grouping. For example, your ViewModel may have something like an ActiveFilterString property which gets applied to the data to determine what is displayed and gets acted upon. The ViewModel doesn't care how the UI decides to handle modifying this ActiveFilterString property, it just needs to know it exists and to use it when taking action. – Rachel May 8 '14 at 19:25
  • @Rachel That might end up being quite a task for grids that give the possibility to filter by displayed text. Say for example that I have a value that's a foreign key to another table, which is represented by a combobox (a pretty common scenario). If it's allowed to filter on that column by displayed text, it means the end user could potentially ask to get every rows where the text for this cell contains the letters "ers". In this case it would be difficult to translate that as a filter for the viewmodel to rely on. – Crono May 8 '14 at 19:30
  • Also, what if the user selects a group of records? How is the viewmodel informed of the active selection? – Crono May 8 '14 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.