WPF鼠标按下事件不会触发

时间:2013-07-24 13:17:55

标签: c# wpf user-controls mousedown

我必须在这里做一些愚蠢的事情但是当我点击UserControl时我无法触发MouseDown事件。让我疯狂。

这是UserControl的XAML:

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

代码背后:

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

点击用户控件时为什么不会出现这种情况?

1 个答案:

答案 0 :(得分:37)

您的MouseDown由RichTextBox处理。请改用PreviewMouseDown。