【问题标题】:Created Bindable WindowsFormsHost, but child update is not being reflected to control创建了可绑定的 WindowsFormsHost,但子更新未反映到控件
【发布时间】:2012-07-16 18:10:46
【问题描述】:

我创建时遇到了一个问题,我想将控件绑定到 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;
        }
    }

e.NewValue 获得了我想要的控件并正确设置它,但我没有看到更改被反映。孩子已设置,但无法看到带有新控件的 windowsFormsHost。

有人知道吗?

感谢和问候, Kev84

【问题讨论】:

    标签: .net wpf binding windowsformshost


    【解决方案1】:

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

    XAML 中的类似内容:

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

    ..这在你的代码隐藏中:

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

    【讨论】:

    • 但如果我这样做,我仍然必须在代码中设置孩子。然后调用 MyWindowsFormHost 的 propertychange。我也看过这个解决方案,但我认为它不如包装解决方案有效。不过谢谢
    • 好的,我明白了,谢谢,是的,显然我走错了路,不过我现在明白了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多