绑定WindowsFormsHost Child属性

Zwi*_*zak 7 c# wpf binding mvvm

我正在创建一个MVVM应用程序.

在我的模型中,我需要处理View中显示的System.Windows.Forms.Panel.我的想法是在ViewModel中创建这个Panel,然后从一侧 - 将View绑定到它,另一边将其句柄传递给模型.

我有一个WindowsFormsHost控件:

<Page x:Class="Test.Views.RenderPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Test.Views"
      mc:Ignorable="d" 
      d:DesignHeight="800" d:DesignWidth="1200"
      Title="Page1">

    <DockPanel>
        <WindowsFormsHost x:Name="winformsHost" Child="{Binding RenderPanel}"/>
    </DockPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

我想将它的Child属性与ViewModel提供的RenderPanel绑定

public ObservableObject<System.Windows.Forms.Panel> RenderPanel { get; private set; }

public VideoRecorderViewModel ()
{
    RenderPanel = new System.Windows.Forms.Panel (); //Bind it here
    var model = new Model (RenderPanel.Handle); pass it to the model
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误说:

System.Windows.Markup.XamlParseException: 
A 'Binding' cannot be set on the 'Child' property of type 'WindowsFormsHost'. 
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

小智 5

该错误是不言自明的。您不能绑定到属于 WPF WindowsFormsHost 控件的子对象的属性

您可以创建一个带有一些附加属性的类来实现这一点: 无法绑定到属于 C#/XAML 应用程序中的 WindowsFormsHost 子对象的属性的解决方法?

或者您可以使用 ContentControl 包装它,如下所示:Created Bindable WindowsFormsHost,但子更新未反映到控件