Workaround for inability to bind to a property that belongs to a WindowsFormsHost Child object in C#/XAML app?

后端 未结 3 1213
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 10:17

I have a C# WPF 4.51 app. As far as I can tell, you can not bind to a property belonging to an object that is the child of a WPF WindowsFormsHost control. (If I a

3条回答
  •  甜味超标
    2021-01-06 11:05

    Create a dependecy object of type Windows Form Host.

    using System.Windows.Forms.Integration;
    
    namespace MainStartUp.DependencyObjects
    {
        public class FormHostDependencyObject : WindowsFormsHost
        {
            public static readonly DependencyProperty ContentControlProperty =
                DependencyProperty.Register("ContentControl", typeof(System.Windows.Forms.Control), 
                    typeof(FormHostDependencyObject),
                    new PropertyMetadata(new System.Windows.Forms.Control(), PropertyChaged));       
    
            public static void SetContentControl(UIElement element, string value)
            {  
                element.SetValue(ContentControlProperty, value);          
            }
    
            public static string GetContentControl(UIElement element)
            {
                return (string)element.GetValue(ContentControlProperty);
            }
    
            private static void PropertyChaged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
            {
                ((FormHostDependencyObject)dependencyObject).Child = (System.Windows.Forms.Control)e.NewValue;
            }
        }
    }
    

提交回复
热议问题