1

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?

Community
  • 1
  • 1
Zamboni
  • 7,897
  • 5
  • 43
  • 52

2 Answers2

1

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.

BlackICE
  • 8,816
  • 3
  • 53
  • 91
  • Can you please point me to an example that shows how to use events from a MFC or VB6 ActiveX control so that I implement the INotifyPropertyChanged as I do have access to the source once I get my prototype working? – Zamboni Nov 13 '11 at 01:55
  • If you're asking how to consume the events from the activex control, you do it just like you would any other object exposing events, i.e. mycontrol.myevent += mycontrol_myeventhandler; If you're asking how to write the activex control to raise events, I've no idea, this may get you started though: http://stackoverflow.com/questions/1455577/how-can-i-make-an-activex-control-written-with-c-sharp-raise-events-in-javascrip – BlackICE Nov 13 '11 at 02:10
1

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.

RS Conley
  • 7,196
  • 1
  • 20
  • 37
  • Here is a site that provides a really [good example](http://binglongx.wordpress.com/2011/01/08/c-application-handles-events-fired-by-activex-step-by-step/) – Zamboni Nov 19 '11 at 16:40