开发者

M-V-VM best practices

开发者 https://www.devze.com 2022-12-31 10:50 出处:网络
I found two ways to use M-V-VM pattern in WPF: allocate the ViewModel into the View\'s code behind (setting it as the DataContext);

I found two ways to use M-V-VM pattern in WPF:

  1. allocate the ViewModel into the View's code behind (setting it as the DataContext);
  2. allocate the ViewModel into a 开发者_开发百科XAML file and create the corresponding view using a DataTemplate.

The Model can be allocated into the ViewModel's constructor.

What do you think about this way of using M-V-VM pattern? What are best practices relative to it?

Thank you


I tend to follow #2 and have found it to be the most flexible. As a best practice I would move these DataTemplate's into ResourceDictionary's so they can be shared amongst XAML that will require them.


Method #3 is what we do:

This is with Boo / Binsor... the datacontext is setup through IOC.

component "AngleRoomModel", IRoomViewModel, AngleRoomViewModel
component "AngleRoom", IRoomView, AngleRoomView:  
  DataContext = @AngleRoomModel
0

精彩评论

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

关注公众号