当你在设置一个按钮要单击又要双击的时候[按正常来说就是两个事件]
事件创建好后,单击控件还正常,就进入单击事件
当双击时,你会发现,它会先去单击事件,随后进入双击事件,就很头痛
【上才艺,花手摇起来】
摇累了,来看看代码吧【一个事件搞定】
代码语言:javascript复制/// <summary>
/// 点击数量
/// </summary>
int CkickCount = 0;
/// <summary>
/// 是否是双击
/// </summary>
bool IsDoubleClick = false;
/// <summary>
/// 当鼠标按下
/// </summary>
private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ChannelCkickCount;
DispatcherTimer ClickTimer = new DispatcherTimer();
ClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
ClickTimer.Tick = (s, e1) =>
{
ClickTimer.IsEnabled = false;
ChannelCkickCount = 0;
if (!ChannelIsDoubleClick) { ClickCommand(); }
ChannelIsDoubleClick = false;
};
ClickTimer.IsEnabled = true;
if (ChannelCkickCount % 2 == 0)
{
ClickTimer.IsEnabled = false;
ChannelCkickCount = 0;
ChannelIsDoubleClick = true;
DoubleClickCommand();
}
}
/// <summary>
/// 单击
/// </summary>
private void ClickCommand()
{
}
/// <summary>
/// 双击
/// </summary>
private void DoubleClickCommand()
{
}
Copy
实现
代码语言:javascript复制//正常操作
按钮的对象.PreviewMouseLeftButtonDown =Button_PreviewMouseLeftButtonDown;
//带参操作【事件方法上也得定义参数哦】
按钮的对象.PreviewMouseLeftButtonDown = delegate (object sender, MouseButtonEventArgs e) {Button_PreviewMouseLeftButtonDown(sender, e, 【这里是传过去的参数】); };
Copy
搞定,点赞收藏加关注哦
“关注[顺网]微信公众号,了解更多更有趣的实时信息”
本文作者:[博主]大顺
本文链接:https://shunnet.top/BJ36bi
版权声明:转载注明出处,谢谢 ☺