1
我正在尝试使用MVVM将Windows窗体控件绑定到WPF中的面板。我的总体目标是能够动态地更改我将使用的具体Windows窗体控件,因为我计划有几个可用的窗体控件。如何使用WPF中的MVVM将Windows窗体控件绑定到“Grid”面板
现在,我已经能够通过让应用程序在初始化时启动一个回调来工作,该回调通过名称访问网格对象。这里是XAML目前的样子:
<Grid Name="WindowsControlObject"></Grid>
回调如下所示:
private void WindowLoaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
this.WindowsControlObject.Children.Add(host);
}
虽然这个作品,我想充分利用MVVM模式,因此是有办法,我可以这样做在XAML /模型视图如下:
XAML:
<Grid Content="{Binding WindowsControl"></Grid>
在我的模式lView:
public class MyModelView
{
public Grid WindowsControl;
public MyModelView{
WindowsControl = new Grid;
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
WindowsControl.WindowsControlObject.Children.Add(host);
}
}
我甚至在我的探索/可能的方法吗?我想到我可能需要使用其他类型的面板(除了网格),但还没有发现任何明显的东西。如果不能完成,我有一个解决方案,只是不是一个很干净的。
做更多挖掘,事实证明,我真的想要将它绑定到“ContentControl”标记,如下所示: XAML: ViewModel: public MyModelView {System.Windows.Forms.Control _myControl; 公共WindowsFormsHost STKObject { 得到 { 返回新WindowsFormsHost(){儿童= _myControl}; } } } –
Aerophilic
不明白你的问题,你想使用绑定将'host'控件添加到'Grid'中? –