Tar
Tar

Reputation: 9015

Events in my UserControl won't fire up - why?

I placed a custom UserControl in my window and set the MouseDoubleClick event inside my usesr-control to change some of its properties.

However, using breakpoints, I realized that the MouseDoubleClick event is never fired. That's true for any event I set in my user-control.

What am I missing ?

btw: I also created some DependencyProperty, "by the book", which works well, if it helps...

Upvotes: 2

Views: 887

Answers (2)

Y.Yanavichus
Y.Yanavichus

Reputation: 2417

Set up background for you User Control. It may be Transparent or White.

Upvotes: 0

Rick Sladkey
Rick Sladkey

Reputation: 34240

Here is how to handle MouseDoubleClick in your UserControl.

Create a new user control called UserControl1. Here is the body of UserControl.xaml:

<Grid Background="Red">
    <!-- leave this blank at first -->
</Grid>

We've set the background to red so we can see that we're working with the user control. Also, it needs a background in order to to receive the click events.

Add a double-click method override in the code-behind for the user control in UserControl1.xaml.cs:

protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
    base.OnMouseDoubleClick(e);
    MessageBox.Show("Double-Click!");
}

Now create a window and add your user control to it, e.g. Window1.xaml:

<Grid>
    <local:UserControl1/>
</Grid>

Run your program so that Window1 is display and the whole window should be red. Double-click on the window and you'll see a message-box:

enter image description here

Once all this is working you can continue with whatever other goal you needed to use the double-click event for.

Upvotes: 1

Related Questions