【发布时间】:2016-11-26 12:13:55
【问题描述】:
我正在创建一个 MVVM 应用程序。
在我的 model 中,我需要 handle 到 System.Windows.Forms.Panel 正在显示在 View。我的想法是在 ViewModel 中创建这个面板,然后从一侧 - 将视图绑定到它,在另一侧,传递它的模型句柄。
我有一个 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 绑定
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.
如何解决这个问题?
【问题讨论】:
-
这个答案应该对你有帮助:stackoverflow.com/questions/11435781/…