Created Bindable WindowsFormsHost, but child update is not being reflected to control
Asked Answered
L

1

8

I created ran into a problem where i want to bind a control to a windowsFormsHost control. But as we all know, the Child property is not a 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;
        }
    }

The e.NewValue gets the control I want and sets it properly, BUT i do not see the change being reflected. The child is set, but can't see the windowsFormsHost with new control.

Any one have idea?

Thanks and Regards, Kev84

Lieabed answered 16/7, 2012 at 18:10 Comment(0)
F
10

Instead of creating a wrapper you could wrap the WindowsFormsHost in a ContentControl and set its Content property via a binding. That way you avoid the issue with the WindowsFormsHosts Child property not being a dependency property.

Something like this in XAML:

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

..and this in your code-behind:

public WindowsFormsHost MyWindowsFormsHost
{   
    get { return new WindowsFormsHost(){Child=myWinformsControl}; }   
}
Flyboat answered 16/7, 2012 at 19:34 Comment(2)
but if i do it this way, i still have to set the child in code. Then call on propertychange for MyWindowsFormHost. i have seen this solution as well, but i don't think it works as well as a wrapper solution. Thanks thoughLieabed
Ok i figured it out, thanks, yea apparently i was on the wrong track, i get it now though, thanks.Lieabed

© 2022 - 2024 — McMap. All rights reserved.