简体   繁体   中英

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...

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, eg 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:

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM