Pros and Cons of WPF Specificity in the View Model

I'm having trouble choosing what to think about this piece of code:

public SolidColorBrush Brush { get { return IsValid ? _validItemBrush : _invalidItemBrush; } } 

This is part of the presentation model in my current project, and as you can imagine, Brush will be tied to some text elements in the user interface to indicate the (un) validity of other parts of the data in an otherwise fairly simple and simple dialog.

Proponents of this piece of code say that since we use WPF, we could also allow some simple WPF constructors in the view model.

Opponents say this violates the Division of Anxiety, as it clearly dictates a style that should be taken care of solely in appearance.

Share your arguments, and if you're unsatisfied with the code above, share your ideas with alternative solutions. (I'm especially interested in what you can say about using DataTemplate s).

Is it possible that there is one solution that can be considered best practice?

+7
source share
3 answers

Personally, I would like two brushes to be defined in XAML, and they have controls that use them to switch brushes (in xaml) based on the IsValid property. This can be done very easily using DataTriggers or even with one IValueConverter processor - the converter can take 2 brushes, as well as logical and swap between them quite easily.

This provides a neutral presentation of business logic - the "Brush" is very specific to a specific presentation form and a clean choice. Hard coding this in the ViewModel violates the principle of single responsibility, and also is not a clear separation of problems.

I would very much keep this in the view and switch to the IsValid (bound) property specific to ViewModel.

+8
source

Although there are situations where I can use WPF constructors in a presentation model, this is not one of them. That's why:

  • More difficult to change. If you define brushes as resources and use them in styles, changing the color scheme of your application may just be a matter of loading another resource dictionary. If you specify hardcode color values โ€‹โ€‹in your view models, you have many different things that you need to change if your end users need different colors.

  • Harder to check. If you want to write a unit test that checks if the property returns the correct brush, you must create a brush in the unit test and compare the values โ€‹โ€‹of the two, since this is a reference type.

  • In many, perhaps even in most cases, this does not make the code simpler or easier to maintain. You are most likely already using the style (assuming you are familiar with the styles) as they simplify everything in WPF. Binding IsValid to brush colors is a matter of adding a DataTrigger style to the style. Which is where anyone supporting this code would expect to find it.

Of course, the times when I use WPF constructors in a view model, for example, have long ceased to wonder if this was a problem if the view model set a property of type Visibility . Please note that none of the above problems apply to this case.

+2
source

In cases like yours where it is purely aesthetic, I use triggers or a visual state manager to change colors.

Sometimes I use colors in my ViewModels, but only if its part of my software specification (for example, the color of the chart that displays the patientโ€™s condition depends on the location). In this case, I use the struct struct struct property, which allows the View to use Color for SolidColorBrush, GradientStop, or whatever it wants. I originally used a line in the #AARRGGBB format to completely remove the WPF dependency, but my more experienced staff did not like it.

0
source

All Articles