c# - 绑定(bind) WindowsFormsHost 子属性

标签 c# wpf mvvm binding

我正在创建一个 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/

相关文章:

c# - WPF:将 ContextMenu 绑定(bind)到 MVVM 命令

c# - 来自 C# 的 DirectX

c# - 找不到资源WPF工具包

wpf - WPF TreeView构造-对每个项目使用 View / View 模型是否很好?

c# - 两个列表对象之间的 Linq 查询

c# - 如何全局应用Serialized属性?

c# - 如何对Android Seekbars做限制,不让用户直接移动?

c# - 为什么 View 构造函数被调用两次?

c# - 在没有客户端请求的情况下在客户端套接字上发送数据

c# - 为什么 ContextMenu Command 和 CommandParameter 在 DataGrid 中不起作用