alansiqueira27
alansiqueira27

Reputation: 8534

How can a UserControl trigger an selectionChanged event to a Window?

I want to make a ColorPicker userControl, and put inside a window.

When the user click in any rectangle of the usercontrol, then I want to return this information to the Window. How can I do this please? Thanks!

Upvotes: 0

Views: 1932

Answers (1)

Fischermaen
Fischermaen

Reputation: 12468

Your Color Picker UserControl has to implement an event that is raised every time the user clicks on one of the rectangles of the UserControl. If you don't know how to implement an event, just comment this answer and I give you an example.

Here is the example: You declare your own event args (if needed) to provide some information in the event:

class RectangleClickedEventArgs : EventArgs
{
    public int SomeValue { get; set; }
}

In your usercontrol you declare the event:

public event EventHandler<RectangleClickedEventArgs> RectangleClicked;

In some condition you raise the event in this way (the thread-safe way:

var temp = RectangleClicked;
if (temp != null)
{
    var e = new RectangleClickedEventArgs{ SomeValue = 42};
    temp(this, e);
}

In your form you subscribe the event:

userControl.RectangleClicked += OnRectangleClicked;

And in the event routine you do your desired action:

private void OnRectangleClicked(object sender, RectangleClickedEventArgs e)
{
    // Do what you want to do
}

Hope that helps...

Upvotes: 2

Related Questions