WPF Mouse Down Event won't fire

kev3kev3 picture kev3kev3 · Jul 24, 2013 · Viewed 12.9k times · Source

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?

Answer

Bill Zhang picture Bill Zhang · Jul 24, 2013

You MouseDown is handled by RichTextBox. Use PreviewMouseDown instead.