开发者

Right way of creating own UserControls in WPF (Csharp)

I have moral question :) I am creating my own controls (deriving UserControl) in WPF. Now there are two ways:

  1. I am using one xaml and one cs file and have there all my stuff
  2. I am loyal to MVVM and create one xaml and cs file for user control plus one cs file for its View Model and I am using MVVM technique. Then I have use my control like this: In xaml:

    <ContentControl Content={Binding MyControl....
    

    In code:

    MyControl = new MyControlViewModel()
    

Till now I used second approach but now I am reading some wise articles and it seems that ma开发者_高级运维ny ppl uses first. So should I mix view and logic while creating controls? Of course I keep MVVM in other parts of app (I try to keep all logic stuff in view model). Couse I am afraid that my cs file will be too long.


My opinion is that, as a rule of thumb, anything that requires an explicit VM is not just a control, it's already a kind of component. The controls should be fairly simple and have no advanced business logic.

Well, so if you see your code needs a VM, you can say that you have written a component, so there's no problem in having a separate VM class for it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜