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