【wpf】MouseLeftButtonDown
今天自定义控件的时候,重写OnMouseLeftButtonDown发现根本不会被触发
后改为OnPreviewMouseLeftButtonDown,就触发成功了!
原因不明,后续再看
翻到了WPF的源码。
dotnet/wpf: WPF is a .NET Core UI framework for building Windows desktop applications. (github.com)
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
// Ignore when in hover-click mode.
if (ClickMode != ClickMode.Hover)
{
e.Handled = true;
// Always set focus on itself
// In case ButtonBase is inside a nested focus scope we should restore the focus OnLostMouseCapture
Focus();
// It is possible that the mouse state could have changed during all of
// the call-outs that have happened so far.
if (e.ButtonState == MouseButtonState.Pressed)
{
// Capture the mouse, and make sure we got it.
// WARNING: callout
CaptureMouse();
if (IsMouseCaptured)
{
// Though we have already checked this state, our call to CaptureMouse
// could also end up changing the state, so we check it again.
if (e.ButtonState == MouseButtonState.Pressed)
{
if (!IsPressed)
{
SetIsPressed(true);
}
}
else
{
// Release capture since we decided not to press the button.
ReleaseMouseCapture();
}
}
}
if (ClickMode == ClickMode.Press)
{
bool exceptionThrown = true;
try
{
OnClick();
exceptionThrown = false;
}
finally
{
if (exceptionThrown)
{
// Cleanup the buttonbase state
SetIsPressed(false);
ReleaseMouseCapture();
}
}
}
}
base.OnMouseLeftButtonDown(e);
}
有的控件(比如Button)当你单击时,触发的是Click事件。
其实MouseLeftButtonDown是被触发了,对于Button这种控件而言这个事件触发了之后,它直接触发Click事件,并且e.Handled = true 这个消息就结束了。
PreviewMouseLeftButtonDown优先级别更高,在MouseLeftButtonDown之前就触发了。
作者:宋桓公
出处:http://www.cnblogs.com/douzi2/
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性