WPF Mouse Down Event won't fire

吃可爱长大的小学妹 提交于 2020-02-01 00:42:07

问题


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:


You MouseDown is handled by RichTextBox. Use PreviewMouseDown instead.



来源:https://stackoverflow.com/questions/17835205/wpf-mouse-down-event-wont-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!