How do I use MVVM from within Windows Forms to display a WPF control

后端 未结 2 1079
北海茫月
北海茫月 2021-01-19 02:44

I have a requirement to integrate a WPF control into an existing Windows Forms app. The simple and easiest way to do this would be to create an ElementHost cont

相关标签:
2条回答
  • 2021-01-19 03:05

    The essence of MVVM is binding. Since you do not have that in Windows Forms, I am afraid you cannot use MVVM in there.

    0 讨论(0)
  • 2021-01-19 03:27

    You can not do that with the designer, but as you add the Child of your ElementHost in code you can directly create and assign the ViewModel. As you commit changes on this ViewModel they get directly reflected in the WPF View.

    MyView view = new MyView();
    MyViewModel model = new MyViewModel();
    view.DataContext = model;
    ElementHost.Child = view;
    
    
    model.SomeBoundProperty = somethingElse;
    //Magic update of the WPF view
    
    0 讨论(0)
提交回复
热议问题