The created WindowsFormsHost binder file, but updating the child does not affect the control

I'm having a problem when I want to bind a control to a windowsFormsHost control. But, as we all know, the Child property is not DP, so I created a wrapper.

/// <summary> /// Bindable version of windows form hosts /// </summary> public class BindableWindowsFormsHost : WindowsFormsHost { /// <summary> /// Max value of the textbox /// </summary> public Control BindableChild { get { return (Control)GetValue(BindableChildProperty); } set { SetValue(BindableChildProperty, value); } } // Using a DependencyProperty as the backing store for Max. This enables animation, styling, binding, etc... public static readonly DependencyProperty BindableChildProperty = DependencyProperty.Register("BindableChild", typeof(Control), typeof(BindableWindowsFormsHost), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnBindableChildChanged))); /// <summary> /// Handles changes to the FlyoutWindowSize property. /// </summary> private static void OnBindableChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((WindowsFormsHost)d).Child = e.NewValue as Control; } } 

e.NewValue receives the control that I want and installs it correctly, but I don’t see the change reflected. The child is installed, but does not see windowsFormsHost with the new control.

Does anyone have an idea?

Thanks and Regards, Kev84

+4
source share
1 answer

Instead of creating a wrapper, you can wrap WindowsFormsHost in a ContentControl and set its Content property through the binding. This way you avoid a problem with a ChildFormsHosts Child property that is not a dependency property.

Something like this in XAML:

 <ContentControl Content="{Binding MyWindowsFormsHost}" /> 

.. and this is in your code:

 public WindowsFormsHost MyWindowsFormsHost { get { return new WindowsFormsHost(){Child=myWinformsControl}; } } 
+7
source

All Articles