What is the best approach to assign Data Context using MVVM?

后端 未结 2 879
借酒劲吻你
借酒劲吻你 2021-02-02 16:04

I was looking for best approach to set data context property. I found three ways

  1. Assign data context on View (either on XAML or code behind).

  2. As

2条回答
  •  有刺的猬
    2021-02-02 17:02

    I personally like this approach because it makes me have to write less code :). It basically uses an IValueConverter to lookup which view to use whenever a wpf control needs to present a ViewModel visually and the IValueConverter sets the datacontext for you. It also shows you how to create a datatemplate that allows you to require WPF to utilize this converter by default, something like this:

    
        
    
    

提交回复
热议问题