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?

Crono
  • 1,617
  • 15
  • 24
  • 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 08 '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 08 '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 08 '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 08 '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 08 '14 at 19:32
  • @Crono In MVVM the is your ViewModel is your application, and the View is just a nice user-friendly interface to sit on top of it. There are many possible solutions to this that depend on your data format, what you're doing with it, what 3rd party controls you're using, etc but the idea is you want to be able to hook up a test script or a command prompt window to the ViewModel and have everything be fully functional and act the same way as it would if attached to a XAML interface. – Rachel May 08 '14 at 19:51
  • @Rachel then technically, it would be okay for the ViewModel to know about a third party control within the view that's being used for data-shaping. Correct? It also means that it's very unlikely for a full ViewModel object to be reused by other views that could potentially be using completely different controls? – Crono May 08 '14 at 19:59
  • @Crono No, because then your ViewModel would be tied to using a specific View/Control, and you'd be mixing the two layers of your application. – Rachel May 08 '14 at 20:02
  • @Rachel then my first impression was right: ViewModel should not know about the View. I just wish I could wrap my mind around how I should expose UI-centric features to the ViewModel without it depending on a specific View or Control. In essence, I'm looking for a way to bind a concept. I just find it very hard to do it properly, efficiently and in a reusable fashion. :/ – Crono May 08 '14 at 20:05

0 Answers0