首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定WindowsFormsHost子属性

绑定WindowsFormsHost子属性
EN

Stack Overflow用户
提问于 2016-07-22 14:30:45
回答 2查看 6.3K关注 0票数 8

我正在创建一个MVVM应用程序。

在我的模型中,我需要在视图中显示的System.Windows.Forms.Panel的句柄。我的想法是在ViewModel中创建这个面板,然后从一边--绑定视图到它--,将它的句柄传递给模型

我有一个WindowsFormsHost控件:

代码语言:javascript
复制
<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>

我想将它的子属性绑定到ViewModel提供的我的ViewModel

代码语言:javascript
复制
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
}

但是,我收到一个错误,它说:

代码语言:javascript
复制
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.

怎么解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-22 16:40:20

错误是不言自明的。不能绑定到属于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

票数 5
EN

Stack Overflow用户

发布于 2018-05-18 04:03:28

创建类型为WindowsFormHost的依赖项属性和对象,并在属性约束事件处理程序中设置子属性。

代码语言:javascript
复制
using System.Windows.Forms.Integration;

namespace MainStartUp.DependencyObjects
{
  public class FormHostDependencyObject : WindowsFormsHost
   {
       public static readonly DependencyProperty ContentControlProperty =
        DependencyProperty.Register("ContentControl", 
         typeof(System.Windows.Forms.Control), 
            typeof(FormHostDependencyObject),
            new PropertyMetadata(new System.Windows.Forms.Control(), 
            PropertyChaged));       

    public static void SetContentControl(UIElement element, string value)
    {  
        element.SetValue(ContentControlProperty, value);          
    }

    public static string GetContentControl(UIElement element)
    {
        return (string)element.GetValue(ContentControlProperty);
    }

    private static void PropertyChaged(DependencyObject dependencyObject, 
       DependencyPropertyChangedEventArgs e)
    {
        ((FormHostDependencyObject)dependencyObject).Child = 
               (System.Windows.Forms.Control)e.NewValue;
     }
   }
 }

在xaml中使用它,如下所示。

代码语言:javascript
复制
<UserControl x:Class="MainStartUp.Views.WIndowsHostUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup- 
         compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MainStartUp.Views"
         xmlns:DependecyObjects="clr- 
         namespace:MainStartUp.DependencyObjects"
         xmlns:wf="clr- 
         namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
   <Grid>
       <!--<ContentControl Content="{Binding ContentControl}" />-->
       <DependecyObjects:FormHostDependencyObject ContentControl="{Binding 
        ContentControl}"></DependecyObjects:FormHostDependencyObject>
   </Grid>
</UserControl>

在我的视图模型中,我有一个object属性,为此我将托管窗口控制对象:

代码语言:javascript
复制
public object ContentControl
    {
        get
        {
            return _contentControl;
        }
        set
        {
            _contentControl = value;
            RaisePropertyChangedEvent("ContentControl");
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38529025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档