本文介绍了创建了Bindable WindowsFormsHost,但子更新未反映到控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
        }
    }

e.NewValue获取我想要的控件并进行设置它正确,但是我看不到变化被反映出来。子级已设置,但看不到带有新控件的windowsFormsHost。

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.

有人知道吗?

感谢和问候,
Kev84

Thanks and Regards,Kev84

推荐答案

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

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.

在XAML中类似这样的事情:

Something like this in XAML:

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

..而这在您的代码背后:

..and this in your code-behind:

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

这篇关于创建了Bindable WindowsFormsHost,但子更新未反映到控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 13:37