绑定WindowsFormsHost子级属性 [英] Binding WindowsFormsHost Child property
问题描述
我正在创建MVVM应用程序.
I am creating an MVVM application.
在我的模型中,我需要句柄到显示在视图.我的想法是在 ViewModel 中创建该面板,然后从一侧-将视图绑定到它,在另一侧,传递其处理模型.
In my model I need handle to a System.Windows.Forms.Panel that is being displayed in the View. My idea is to create this Panel in the ViewModel and then from one side - bind the View to it, on the other side, pass its handle to the model.
我有一个WindowsFormsHost控件:
I've got an WindowsFormsHost control:
<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>
我想将其Child属性与ViewModel提供的RenderPanel绑定
And I would like to bind it's Child property with my RenderPanel provided by ViewModel
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
}
但是,我收到一个错误消息:
However, I am getting an error saying that:
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.
该如何解决?
推荐答案
该错误不言自明.您不能绑定到属于WPF WindowsFormsHost控件的子级对象的属性
The error is self explanatory. You can't bind to a property belonging to an object that is the child of a WPF WindowsFormsHost control
您可以创建一个具有一些附加属性的类来实现此目的: 无法绑定到的解决方法属于C#/XAML应用程序中WindowsFormsHost子对象的属性?
You could create a class with some attached properties to achieve this: Workaround for inability to bind to a property that belongs to a WindowsFormsHost Child object in C#/XAML app?
或者您也可以使用ContentControl对其进行包装,如下所示:
Or you could wrap it using a ContentControl like here: Created Bindable WindowsFormsHost, but child update is not being reflected to control
这篇关于绑定WindowsFormsHost子级属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!