开发者

working with WPF usercontrol and MVVM

I have following questions

  1. Should the consumer of my usercontrol assign the usercontrol's DataContext or set some dependency property. (related to #3 : if DataContext then my individual items need to bind directly to the object given in DC, if DP then I have the luxury to have bind to any VM)
  2. If they set property, and if I am using 3 primitive items, should I accept the开发者_Go百科m as individual properties or combine them together to a Model for my usercontrol
  3. Should I ask the consumer of my usercontrol to send me model or viewmodel ( I say viewmodel but for all the controls I have used so far, I have never seen anybody asking me to send them VM - I am sure some could be implementing MVVM internally


Your consumer wants a user control. So I presume the user control should be able to work in any context/application(WPF). So, to answer your questions

1) The consumer should set dependency properties which is defined in the user control. By using the datacontext you will be coupling the usercontrol to the consumer.

2)Take them as individual primitive properties, otherwise the consumer needs to create an object unnecessarily to cater with your model(coupling again-why should the consumer need to know about your model?).

3)No, you should not ask the cosumer to send you the view model.Why do you need to know which consumer is using your "generic" user control.

If you cannot do any of the above because of practical considerations - then dont worry about breaking any/all the rules because your user conrol is coupled with a specific context-it is not generic any more. If you write a generic user control, any WPF application can use your user control. It is your call.


1. I would say this depends on the kind of UserControl, if it is "generic" you should be able to change the DataContext as the control internally should not have anything to do with the DataContext. For example if i create an ImageButton UserControl which exposes the properties Caption and ImageSource then those should be bound internally independent of the DataContext, the on the instance those can be bound and the DataContext may be changed as well, e.g.

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

Here one could then change 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 a viewmodel i would expect the UserControl to bind to viewmodel properties internally, relative to the DataContext.

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

<v:StatisticsView />

If the viewmodel to be passed is in a property of the current DataContext you may bind the DataContext as well:

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

2. This can be handled either way i would say, especially if you have only three properties its not too much of a hassle to create those. You might want to consider some aspects like dependency, e.g. does it make sense to group all three propeties in an object?


3. As noted in 1. this should be apparent from the UserControl itself, if it's a StatisticsView the consumer should pass in a StatisticsViewModel (either implicitly by inheriting the current DataContext or by binding it explicitly).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜