Work with user management of WPF and MVVM

I have the following questions

  • If the user of my usercontrol assigns a usercontrol DataContext or sets some dependency property. (related to No. 3: if DataContext, then my individual elements should be directly attached to the object specified in DC, if DP then I have the luxury to have a binding to any VM)
  • If they set a property, and if I use 3 primitive elements, should I accept them as separate properties or combine them together with Model for my usercontrol
  • Should I ask the consumer of my usercontrol to send me a model or viewmodel (I say viewmodel, but for all the controls that I have used so far, I have never seen anyone ask me to send them a VM - I'm sure some may be the implementation of MVVM inside
+4
source share
2 answers

Your consumer wants to control the user. Therefore, I assume that the user control should work in any context / application (WPF). So, to answer your questions

1) The user must set the dependency properties defined in the user control. Using a datacontext, you will associate user control with the consumer.

2) Take them as individual primitive properties, otherwise the consumer needs to create an object without having to service your model (reconnecting - why should the consumer know about your model?).

3) No, you should not ask comumer to send you a viewing model. Why you need to know which consumer is using your β€œgeneric” user control.

If you cannot do any of the above due to practical reasons - then do not worry about breaking any / all rules, because your user connector is associated with a specific context - it is no longer common. If you are writing a generic user control, any WPF application can use your user control. This is your call.

+2
source

1. I would say that it depends on the type of UserControl, if it is "general", you should be able to modify the DataContext, since the internal control should not have anything to do with the DataContext. For example, if I create an ImageButton UserControl that provides the Caption and ImageSource , then they must be connected independently of the DataContext, and on the instance they can be connected, and the DataContext can also be changed, for example

 <uc:ImageButton Caption="{Binding ButtonInfo.Caption}" ImageSource="{Binding ButtonInfo.Image}"/> 

Here one could modify the DataContext to simplify the bindings a bit:

 <uc:ImageButton DataContext="{Binding ButtonInfo}" Caption="{Binding Caption}" ImageSource="{Binding Image}"/> 

If, on the other hand, the UserControl is a view for the view model, I would expect the UserControl to bind to the viewmodel properties inside, relative to the DataContext.

So, in the DataTemplate, where the current DataContext is already the viewmodel of this view, a simple instance should do without any action, i.e.

 <v:StatisticsView /> 

If the transmitted view model is in the property of the current DataContext, you can also bind the DataContext:

 <v:StatisticsView DataContext="{Binding StatisticsViewModel}"/> 

2. This can be processed as I would say, especially if you have only three properties, it is not too much trouble to create them. You might want to consider some aspects, such as dependency, for example. Does it make sense to group all three propositions in an object?


3. As noted in 1. this should be obvious from the UserControl itself, if it is a StatisticsView , the user must pass it to the StatisticsViewModel (either implicitly, inheriting the current DataContext, or directly binding it).

0
source

Source: https://habr.com/ru/post/1311611/


All Articles