-1

I have a Page, inside I generate a few copies of a UserControl. The page is supposed to do stuff when the UserControl_MouseDown event is fired. Everything works fine until the user clickes on a part of the UserControl where a Control is present. As you would expect, the Control prevents the UserControl_MouseDown event to fire.

Page

//Fired when the StackPanel is clicked but not when the TextBox is.
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
    //Do stuff
}

UserControl

<StackPanel>
    <TextBox/>
</StackPanel>

Sadra M.
  • 1,304
  • 1
  • 17
  • 28

1 Answers1

2

As controls tend to swallow events you could rather use the PreviewMouseDown-Event:

<UserControl PreviewMouseDown="UserControl_MouseDown">
    <StackPanel>
        <TextBox />
    </StackPanel>
</UserControl>

Read this: https://learn.microsoft.com/en-us/dotnet/api/system.windows.uielement.previewmousedown?view=netframework-4.8

Faenrig
  • 197
  • 1
  • 9
  • 1
    ach, no that's not what I meant. – Sadra M. Jan 28 '20 at 10:20
  • 1
    Ah I think I now know what you tried to ask. Do you have the `UserControl_MouseDown` in your ``? I missed that, sorry. Yeah the event should span all over the User Control obviously. You should use the PreviewMouseDown-Event instead as it won't be "swallowed" by controls. I'll update the answer. – Faenrig Jan 28 '20 at 14:18