Binding a WPF element host to a WinForm property - wpf

I have a WPF element host sitting in a WinForm.
I would like to bind a property of the element to a property of the WinForm.
In my searches I found the opposite solution, binding between WinForms to WPF properties (when the WinForm is sitting on a WPF host).
Here is an example:
How to bind a windows control in WPF????
I also saw this answer (not exactly an answer, I'd say a conclusion): Binding to a WPF hosted control's DependencyProperty in WinForms
It did not help me. I also want to keep the the principle of MVVM in the WPF element host.
|It there a way to do this?

Finally I solved it this way:
I created a property in ViewModel and init him through the WinForm when I use the wpf host control in the first time.
I binding the property to element in xaml
and that's all.

Related

Binding winform control property in XAML

We are currently in the process of switching our product from WinForms to WPF. At the moment we are using some 3rd party WinForm controls that are required for our application. Even though we plan to eventually replace them with WPF versions, right now this is not possible. We've tried hosting them in the wpf window inside WindowsFormsHost control, and it seems to work just fine. The only problem we have is how to pass our data from VM to these controls. We would like to avoid any code-behind and alterations to VM just to accomodate this controls. Ideally, we would prefer to keep VM completely unaware of the controls used to display it's data, so that when we do change to WPF version of these controls, we only need to modify the view. This is why we're looking for a way to bind VM property to hosted WinForm control from XAML. If this helps, we can certainly live with the fact that there is only a one way binding from VM to the control, and we don't mind if the binding works only once, without the subsequent updates from VM, since the VM properties we are binding do not change. Perhaps someone has any ideas how we can make this happen?
Not sure if there is a better way, but here's one idea:
Wrap your WinForm control/WindowsFormsHost control into a wrapper control (inherit from Control or use a UserControl, whatever is best for you).
On this wrapper you can add dependency properties that you want to bind to your VM.
Inside the wrapper code, you can add the boilerplate required to propagate changes back and forth between your wrapper dependency properties and your winform properties.
This hides the dirt under the carpet and exposes a nice WPF facade that you can bind to as usual, without changing your VM.
When the control is phased out, remove the wrapper from your project and you can bind the VM directly to the new WPF replacement control.

Wpf binding of TextBox inside ElementHost not updating when click on win forms control

I have a win forms application on which an WPF UserControl is hosted using ElementHost. When user changes the text of textbox inside WPF user control and click on any Win forms control it does not update the binded property of the ViewModel.
It is not possible to change the UpdateSourceTrigger to PropertyChanged because for each change in ViewModel I need to add an undo entry. I have read some articles saying that win forms and wpf have their own focus contexts which are not shared. Anyone has found any workaround for this ?
Thanks
Sameera

How do I configure a WinForms TabControl inside the WindowsFormsHost WPF control?

How do I configure a WinForms TabControl inside the WindowsFormsHost WPF control, how can add TabPages using XAML?
Thanks.
So far as I understand it, you can create the WinForms TabPages in the same way you would create something normally in WPF with XAML. See this page for an idea of what I mean.
We solved encapsulating the TabControl and its tabpages inside a UserControl, then adding the UserControl to the WindowsFormsHost.

How to implement two way binding between an ActiveX control and a WPF MVVM View Model

I have a WPF application implemented using the MVVM framework that uses an ActiveX control and I need to keep the WPF and ActiveX UI synchronised.
So far I can update the ActiveX UI when I change the WPF UI using the code at the bottom of the question that I got from the article Hosting an ActiveX Control in WPF and this question.
But I cannot update the WPF UI when I make a change in the ActiveX UI.
I suspect that I need to fire the PropertyChanged event from my ActiveX control but I have no idea how to do this or if it is even possible.
The ActiveX controls I have written are in VB6 and MFC as I am just prototying at this time for the eventual integration of VB6 ActiveX controls in a WPF contaner application.
Here is a code snipet that indicates the work done so far:
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the ActiveX control.
AxTEXTBOXActiveXLib.AxTEXTBOXActiveX axWmp = new AxTEXTBOXActiveXLib.AxTEXTBOXActiveX();
// Assign the ActiveX control as the host control's child.
host.Child = axWmp;
axWmp.DataBindings.Add(new System.Windows.Forms.Binding("ActiveXStatus", (MainWindowViewModel)this.DataContext, "ModelStatus", true, DataSourceUpdateMode.OnPropertyChanged ));
// Add the interop host control to the Grid
// control's collection of child controls.
this.activexRow.Children.Add(host);
How to implement two way binding between an ActiveX control and a WPF MVVM View Model?
What you need to do is create an ActiveX library that both the ActiveX control can refer too and the WPF assembly. In the library create an interface with a refresh method of whatever complexity you need. Create a global multi-use method that objects that implement can use to register themselves. Then in the ActiveX control it can check to see whether anything been registered and fire the refresh method. Then in the WPF Assembly the UI can implement the interface and register itself supplying the connection between it and the ActiveX Control.
You're going to have to find some way of notifying WPF that data in the ActiveX control has changed. This is usually handled in WPF by implementing INotifyPropertyChanged or using ObservableCollections. Just binding a property doesn't update the UI for WPF either, try binding something in your view to a property on the viewmodel and not using INotifyPropertyChanged. If you have control of the activex source this should be easy to do using events.

How to bind a windows control in WPF?

I'm using WPF 4.0. I have a WPF Datagrid. One of my column in a datagrid is a template column. In that template column I have used Win forms Textbox and my problem is that,
How to bind that win forms texbox control in WPF?
How to access that control or column in Code behind (c#)?
You can't use WPF data binding with WinForms controls, WPF data binding requires dependency properties and FrameworkElement derived objects - both are part of WPF and not available in WinForms.
WinForms has it's own data binding system, it's completely incompatible with WPF's data binding (it's also weaker and unusable in some cases).
If you want to use WPF's databinding you have to use only WPF controls.
I suggest you either use the WPF TextBox or switch the entire grid to WinForms and use DataGridView.
by the way - I wouldn't put a WinForms control inside a WPF data grid (or any other items control) - I suspect this will give you a lot of trouble in the future.

Resources