2

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

4

1 回答 1

1

NavigationinUserControl和 in 的实例MainWindow不同。 UserControl为其自己的实例触发事件。同时,从自己的实例中MainWindow监听事件。

要么使用 的单个实例Navigation,要么在 中创建代理事件UserControl并订阅它MainWindow

此外,您不需要声明您的EventHandler. 它已经宣布了。

于 2014-08-07T09:12:23.587 回答