当我的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); } }When my usercontrol(WindowScreen) loads for the first time, my elementhost displays correctly. When I instantiate the usercontrol and pass in a different id, the elementhost doesnt get updated. Is there a reason why for this or is there a way to fix this?
Here's my code.
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); } }最满意答案
当你这样称呼时:
WindowScreen ws = new WindowScreen(298);您正在创建一个新的WindowScreen ,但从未将其设置为在TestScreen控件中使用。 你需要用这个覆盖当前的WindowScreen - 例如:
// This needs to be the appropriate/correct variable this.windowScreen1 = new WindowScreen(298);When you call this:
WindowScreen ws = new WindowScreen(298);You're creating a new WindowScreen, but never setting it to be used in your TestScreen control. You need to overwrite the current WindowScreen with this one - something like:
// This needs to be the appropriate/correct variable this.windowScreen1 = new WindowScreen(298);更多推荐
发布评论