如何在winform中刷新wpf elementhost(how to refresh wpf elementhost inside winform)

编程入门 行业动态 更新时间:2024-10-28 20:19:06
如何在winform中刷新wpf elementhost(how to refresh wpf elementhost inside winform)

当我的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);

更多推荐

本文发布于:2023-08-02 05:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   winform   wpf   elementhost   refresh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!