I have read through a lot of articles to resolve this problem, but I can't get this to work.
The Situation: I have a WPF MainWindow where I am loading UserControls into it. The Mainwindow should listen to an EventHandler in a Class called Navigation. The Event will be fired upon certain changes in the UserControls.
So far I can confirm that the UserControls can fire the Event, but the MainWindow does not pickup the Throw.
Navigation Class:
public class Navigation
{
public delegate void EventHandler(object sender, EventArgs args);
public event EventHandler ThrowEvent = delegate { };
public void NavigationUpdateEvent()
{
ThrowEvent(this, new EventArgs());
}
}
UserControl:
public delegate void EventHandler(object sender, EventArgs args);
public event EventHandler ThrowEvent = delegate { };
Navigation myNavigation = new Navigation();
public myUserControl()
{
InitializeComponent();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
if (cbCheckBox.IsChecked.Value)
{
UMENavigation.NavigationUpdateEvent();
}
}
The MainWindow:
private void DoSomething()
{
MessageBox.Show("It worked!");
}
private Navigation _Thrower;
public MainWindow()
{
InitializeComponent();
_Thrower = new Navigation();
_Thrower.ThrowEvent += (sender, args) => { DoSomething(); };
}
However, the _Thrower never picks up the Fired Event..
Any help is greatly appreciated! This is starting to hurt :)
Greetings, Tom
Navigation
instances in UserControl
and in the MainWindow
are different.
UserControl
fires the event for its own instance. At the same time, MainWindow
listens for the event from its own instance.
Either use single instance of Navigation
, or create a proxy-event in UserControl
and subscribe it in MainWindow
.
Besides, you don't need to declare your EventHandler
. It's already declared.