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

后端 未结 2 1081
北海茫月
北海茫月 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: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
    

提交回复
热议问题