2016-07-22 68 views
3

我正在創建一個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. 

如何解決這個問題?

+0

這個答案應該可以幫助你:http://stackoverflow.com/questions/11435781/a-binding-can-only-be-set-on-a-dependencyproperty-of-a-dependencyobject – alessalessio

回答

3

錯誤是不言自明的。你不能綁定到屬於對象,它是一個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