WPF 鼠标按下事件不会触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17835205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Mouse Down Event won't fire
提问by kev3kev3
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.
我一定在这里做了一些愚蠢的事情,但是当我单击 UserControl 时,我无法触发 MouseDown 事件。快把我逼疯了。
Here's the XAML for the UserControl:
这是 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>
Code Behind:
背后的代码:
private void cusTextBoxControl_MouseDown(object sender, MouseButtonEventArgs e)
{
selected = (CustomTextBox)sender;
}
Why wont this fire when I am clicking the User Control?
为什么当我单击用户控件时不会触发?
回答by Bill Zhang
You MouseDown is handled by RichTextBox. Use PreviewMouseDown instead.
您的 MouseDown 由 RichTextBox 处理。请改用 PreviewMouseDown。

