我正在创建一个 MVVM 应用程序。
在我的模型中,我需要句柄 System.Windows.Forms.Panel 显示在View强>。我的想法是在 ViewModel 中创建这个面板,然后从一侧 - 将 View 绑定(bind)到它,在另一侧,传递它模型句柄。
我有一个 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>
我想将它的 Child 属性与我的 ViewModel 提供的 RenderPanel 绑定(bind)
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.
如何解决这个问题?
最佳答案
错误是不言自明的。您不能绑定(bind)到属于 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
关于c# - 绑定(bind) WindowsFormsHost 子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529025/