How can I trigger alerts from ViewModel?

I asked this question to several colleagues who, in my opinion, have a good design. Interestingly, I received different answers. So which option do you think is best suited for the MVVM pattern and why?

Let's say I have a requirement to show a warning icon when $ is more than $ 10k.

  • Option 1. The VM has the Sum property, the XAML trigger and / or binding determines the visibility of the element.
  • Option 2: VM has the "ShowAlert" property, XAML binds visibility using the bool converter for visibility
  • Option 3: VM has the "AlertVisibility" property, XAML is directly linked
  • Option 4: Other (please explain)

Thanks for the feedback!

+5
source share
2 answers

In your case, you have a specific business rule Shown WHEN > 10k, it should be encapsulated in the ViewModel, not in the View, because the View knows how the data will be displayed, not WHEN and WHY.

So, I suggest exposing the properties Amountand ShowAlertand avoiding the disclosure of the property AlertVisibility, because it scatters such a WPF. Check out specific things like Visibility enum states (Hidden, Collapsed, ...)

AlertVisibility, - , WPF Visibility, ViewModel, /, View , MVVM, View ViewModel. bool AlertVisible.

:

! AmountVisible IsAmountSpecified - .

+4

. , 10000. .

VM , , , , .

0

All Articles