C# 如何将Windows窗体控件绑定到";网格“;在WPF中使用MVVM的面板

C# 如何将Windows窗体控件绑定到";网格“;在WPF中使用MVVM的面板,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我正在尝试使用MVVM将Windows窗体控件绑定到WPF中的面板。我的总体目标是能够动态更改我将使用的特定Windows窗体控件,因为我计划有几个可用的控件 现在,我已经能够通过让应用程序在初始化时启动回调来实现这一点,该回调通过名称访问网格对象。以下是XAML当前的外观: <Grid Name="WindowsControlObject"></Grid> 虽然这样做有效,但我正在尝试充分利用MVVM模式,因此有没有一种方法可以在XAML/ModelView中执行以下

我正在尝试使用MVVM将Windows窗体控件绑定到WPF中的面板。我的总体目标是能够动态更改我将使用的特定Windows窗体控件,因为我计划有几个可用的控件

现在,我已经能够通过让应用程序在初始化时启动回调来实现这一点,该回调通过名称访问网格对象。以下是XAML当前的外观:

<Grid Name="WindowsControlObject"></Grid>
虽然这样做有效,但我正在尝试充分利用MVVM模式,因此有没有一种方法可以在XAML/ModelView中执行以下操作:

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);
    }
}
XAML:


我的探索/可能的方法是否正确?我突然想到,我可能需要使用其他类型的面板(网格除外),但还没有发现任何明显的问题。如果无法完成,我有一个解决方案,只是不是一个非常干净的解决方案。

通过进一步挖掘,我发现我确实想将其绑定到一个“ContentControl”标记,如下所示:

XAML:


通过进一步挖掘,我发现我确实想将其绑定到一个“ContentControl”标记,如下所示:XAML:ViewModel:public MyModelView{private System.Windows.Forms.Control{u myControl;public WindowsFormsHost STKObject{get{return new WindowsFormsHost(){Child=\u myControl};}}}}不太理解您的问题,您想使用绑定将
host
控件添加到
Grid
中吗?
<Grid Content="{Binding WindowsControl"></Grid>
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 Content="{Binding WindowsControl}"/>
    private System.Windows.Forms.Control _myControl;

    public WindowsFormsHost STKObject
    {
        get 
        {
            return new WindowsFormsHost() { Child = _myControl};
        }
    }