绑定WindowsFormsHost子属性
问题描述:
我正在创建一个MVVM应用程序。绑定WindowsFormsHost子属性
在我模式我需要手柄到System.Windows.Forms.Panel正在显示在查看。我的想法是创建这面板中视图模型,然后从一个侧面 - 查看绑定到它,在另一边,通过它句柄模式。
我有一个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>
而且我想绑定它的子属性与视图模型提供我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
}
不过,我得到一个错误说:
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控件的子属性
你可以创建一个类的一些附加属性来实现这一目标: Workaround for inability to bind to a property that belongs to a WindowsFormsHost Child object in C#/XAML app?
或者你可以用它包一个ContentControl喜欢这里:Created Bindable WindowsFormsHost, but child update is not being reflected to control
+0
谢谢,我使用了上面链接的ContentControl解决方案。它像一个魅力。 – Zwierzak
这个答案应该可以帮助你:http://*.com/questions/11435781/a-binding-can-only-be-set-on-a-dependencyproperty-of-a-dependencyobject – alessalessio