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?
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
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With