Ok, so I am embedding a microsoft drag and drop application into a WPF app. The drag and drop functionality is very difficult to achieve in the ViewModel, so I have it in the code behind. But now I need to open a UserControl popup window from the code behind. My attempt was to call a method in the ViewModel from the code behind to try and publish a new window event. Unfortunately, I get a null reference due to an unassigned IEventAggregator. Since I am using Caliburn.Micro, getting into the ViewModel is a priority and I want to get the event to publish.
In summation, how can I publish an event in the ViewModel from the code behind (without getting a null reference on the IEventAggregator)?
Code Behind:
public partial class SomeView : System.Windows.Controls.UserControl
{
IEventAggregator events;
_model = new SomeViewModel();
....
private void ShapeDoubleClick(object sender, AxMicrosoft.Office.Interop.VisOcx.EVisOcx_MouseUpEvent e)
{
if (App.VisioControl.Document.Application.ActiveWindow.Selection.Count > 0)
{
_model.PublishEvent(events);
}
}
SomeViewModel:
public void PublishEvent(IEventAggregator events)
{
events.Publish(new NewWindowEvent("SomeOtherViewModel"));
}
I had to get the EventAggregator into the code behind and pass it into the ViewModel to kick off the NewWindow event. I ended up using IoC calls in CM.
IEventAggregator ie = IoC.Get<IEventAggregator>();