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

后端 未结 3 1173
被撕碎了的回忆
被撕碎了的回忆 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:03

    You can only achieve a very, very limited binding with a Windows Forms control as they binding won't receive update notifications and may need to be explicitly polled to get the results via a custom RefreshValues() method or something that polls each piece of data.

    But if all you need is to access the child control, you should do the binding in code:

    (WFH.Child as MyWinFormsControl).Text
    

    If you intend to do a lot of binding, it might be easier to create a WPF wrapper object (UserControl perhaps) that has all the properties you need as DependencyProperties and the underlying code each property would manually poll the WinForms control as if it were the backing field for the property. It is a little complicated at first, but easier than manually polling each property.

提交回复
热议问题