【问题标题】:Binding WindowsFormsHost Child property绑定 WindowsFormsHost 子属性
【发布时间】:2016-11-26 12:13:55
【问题描述】:

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

在我的 model 中,我需要 handleSystem.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.

如何解决这个问题?

【问题讨论】:

标签: c# wpf mvvm binding


【解决方案1】:

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

【讨论】:

  • 谢谢,我使用了上面链接的 ContentControl 解决方案。它就像一个魅力。
【解决方案2】:

创建一个 WindowsFormHost 类型的依赖属性和对象,并在属性 chaged 事件处理程序中设置子属性。

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 中使用它,如下所示。

<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>

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

public object ContentControl
    {
        get
        {
            return _contentControl;
        }
        set
        {
            _contentControl = value;
            RaisePropertyChangedEvent("ContentControl");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多