WPF Mouse Down Event won't fire

2020-06-08 13:49发布

I must be doing something stupid here but I cant get a MouseDown event to fire when I am clicking on the UserControl. Driving me Mad.

Here's the XAML for the UserControl:

<UserControl x:Name="cusTextBox" x:Class="StoryboardTool.CustomTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" MouseDown="cusTextBoxControl_MouseDown">
    <Grid>
        <RichTextBox x:Name="richTextBox">
            <RichTextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem x:Name="ContextMenuBringForward" Header="BringForward" Click="ContextMenuBringForward_Click"/>
                    <MenuItem x:Name="ContextMenuSendBackward" Header="SendBackward" Click="ContextMenuSendBackward_Click"/>
                </ContextMenu>
            </RichTextBox.ContextMenu>
        </RichTextBox>
    </Grid>
</UserControl>

Code Behind:

private void cusTextBoxControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            selected = (CustomTextBox)sender;
        }

Why wont this fire when I am clicking the User Control?

1条回答
我命由我不由天
2楼-- · 2020-06-08 13:53

You MouseDown is handled by RichTextBox. Use PreviewMouseDown instead.

查看更多
登录 后发表回答