如何在winform中刷新wpf elementhost

时间:2012-08-08 19:45:43

标签: c# wpf

当我的usercontrol(WindowScreen)第一次加载时,我的elementhost正确显示。当我实例化usercontrol并传入不同的id时,elementhost不会更新。有没有理由为此或有办法解决这个问题?

这是我的代码。

WindowScreen.cs - winform:

public partial class WindowScreen : UserControl
{
    private WindowView _windowView;
    private WindowViewModel _windowViewModel = null;

    public WindowScreen(int id)
    {
        InitializeComponent();

        elementHost.Child = this.elementHost1;
        _windowViewModel = new WindowViewModel();
        _windowView = (WindowView) this.elementHost.Child;
        //_windowViewModel.LoadTypes(123); --- first load
                  _windowViewModel.LoadTypes(id); --- pass in parameter
        _windowView.DataContext = _windowViewModel;
    }
}

TestScreen.cs - winform:

public partial class TestScreen : UserControl
{
    public TestScreen()
    {
        InitializeComponent();  
    }

    private void button1_Click(object sender, EventArgs e)
    {
        WindowScreen ws = new WindowScreen(298);
    }
}

1 个答案:

答案 0 :(得分:0)

当你这样称呼时:

WindowScreen ws = new WindowScreen(298);

您正在创建新的WindowScreen,但从未将其设置为在TestScreen控件中使用。你需要用这个覆盖当前的WindowScreen - 例如:

// This needs to be the appropriate/correct variable
this.windowScreen1 = new WindowScreen(298);