Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding WindowsFormsHost Child property

Tags:

c#

mvvm

binding

wpf

I am creating an MVVM application.

In my model I need handle to a System.Windows.Forms.Panel that is being displayed in the View. My idea is to create this Panel in the ViewModel and then from one side - bind the View to it, on the other side, pass its handle to the model.

I've got an WindowsFormsHost control:

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

And I would like to bind it's Child property with my RenderPanel provided by ViewModel

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
}

However, I am getting an error saying that:

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.

How to fix this?

like image 437
Zwierzak Avatar asked Jul 22 '16 14:07

Zwierzak


2 Answers

The error is self explanatory. You can't bind to a property belonging to an object that is the child of a WPF WindowsFormsHost control

You could create a class with some attached properties to achieve this: Workaround for inability to bind to a property that belongs to a WindowsFormsHost Child object in C#/XAML app?

Or you could wrap it using a ContentControl like here: Created Bindable WindowsFormsHost, but child update is not being reflected to control

like image 145
Henrique Forlani Avatar answered Oct 18 '22 13:10

Henrique Forlani


Create a dependency property and object of type WindowsFormHost and set the child property in property chaged event handler.

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

use it in your xaml as below.

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

and in my view model i have a object property for which i will have window control object to be hosted:

public object ContentControl
    {
        get
        {
            return _contentControl;
        }
        set
        {
            _contentControl = value;
            RaisePropertyChangedEvent("ContentControl");
    }
}
like image 28
Shivu Suputhra Avatar answered Oct 18 '22 12:10

Shivu Suputhra