David
David

Reputation: 4117

Raise a event in the UserControl and listening on it in the mainwindow

I have a UserControl A.

In the code behind file I want to raise a particular event (created by me).

Now I want that the Main Window (which contains the user control) is listening to the event.

In WindowsForms i used this way:

namespace MyProgramm
{
    public partial class MyClass
    {   
        public MyClass()
        {
            InitializeComponent();
            DataContext = this;
        }

        internal event MyEventHandler MyEvent;

        private void RaiseMyEvent()
        {
            if (MyEvent!= null)
            {
                MyEvent(this, ...);
            }
        }

        public string Name { get; set; }

    }

    internal delegate void MyEventHandler (MyClass sender, ...);
}

How can I solve this in a nice way in WPF?

Upvotes: 0

Views: 159

Answers (1)

Clemens
Clemens

Reputation: 128013

In my opinion, nicer would by to use EventHandler instead of your own delegate type.

Also, if the class is public and has a public constructor, you might make MyEvent public.

public event EventHandler<MyEventArgs> MyEvent;

Upvotes: 1

Related Questions