创建了可绑定的WindowsFormsHost,但子更新未反映到控件中

Kev*_*v84 8 .net wpf binding windowsformshost

我创建遇到了一个问题,我想将控件绑定到一个windowsFormsHost控件.但是众所周知,Child属性不是DP,所以我创建了一个包装器.

/// <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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

e.NewValue得到我想要的控件并正确设置它,但我没有看到反映的变化.孩子已设置,但无法通过新控件查看windowsFormsHost.

有谁有想法?

谢谢和问候,Kev84

Pet*_*sen 9

您可以将WindowsFormsHost包装在ContentControl中,并通过绑定设置其Content属性,而不是创建包装器.这样就可以避免WindowsFormsHosts子属性不是依赖属性的问题.

在XAML中有类似的东西:

<ContentControl Content="{Binding MyWindowsFormsHost}" />
Run Code Online (Sandbox Code Playgroud)

..在你的代码隐藏中:

public WindowsFormsHost MyWindowsFormsHost
{   
    get { return new WindowsFormsHost(){Child=myWinformsControl}; }   
}
Run Code Online (Sandbox Code Playgroud)