What is the advantage of setting DataContext in code instead of XAML?

后端 未结 7 1303
小鲜肉
小鲜肉 2020-12-09 10:54

There seem to be two main ways to define DataContext in WPF:

  • either in code like this:

App.xaml.cs (taken from the WPF MVVM Too

7条回答
  •  醉话见心
    2020-12-09 11:59

    I don't like the idea of having Expression Blend try to instantiate my data objects.

    I set the DataContext through code where I am able to use Dependency Injection to inject the proper objects, services, providers or what else I am using to find my code.

提交回复
热议问题