1

当我的 usercontrol(WindowScreen) 首次加载时,我的 elementhost 显示正确。当我实例化用户控件并传入不同的 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);
    }
}
4

1 回答 1

0

当你调用这个时:

WindowScreen ws = new WindowScreen(298);

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

// This needs to be the appropriate/correct variable
this.windowScreen1 = new WindowScreen(298);
于 2012-08-08T19:56:47.683 回答