news 2026/1/22 6:38:29

wpf自定义控件 ToggleButton_Checked事件怎么防止鼠标滚动误触发

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
wpf自定义控件 ToggleButton_Checked事件怎么防止鼠标滚动误触发
<!-- HandyControl 局部指定控件使用,防止覆盖默认样式 --><UserControl x:Class="Module.PEIS.ZhiYeBingTiJian.View.ToggleButtonCustuom"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:Module.PEIS.ZhiYeBingTiJian.View"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"d:DataContext="{d:DesignInstance local:ToggleButtonCustuom, IsDesignTimeCreatable=False}"d:DesignHeight="30"d:DesignWidth="70"mc:Ignorable="d"><Grid><!-- HandyControl 局部指定控件使用,防止覆盖默认样式 --><Grid.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><!-- HandyControl基础皮肤 --><ResourceDictionarySource="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/><!-- HandyControl主题样式 --><ResourceDictionarySource="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/></ResourceDictionary.MergedDictionaries><!-- 覆盖主题色 --><SolidColorBrush x:Key="PrimaryBrush"Color="#3190FF"/><!--<SolidColorBrush x:Key="SecondaryBrush"Color="#2196F3"/>--><Style x:Key="CustomToggleButton"BasedOn="{StaticResource ToggleButtonSwitchBaseStyle}"TargetType="ToggleButton"><!-- 默认状态颜色 --><!--<SetterProperty="Background"Value="#ccc"/><SetterProperty="Foreground"Value="White"/><SetterProperty="BorderBrush"Value="#FF4081"/>--><!-- 状态触发器:选中状态 --><Style.Triggers><TriggerProperty="IsChecked"Value="True"><SetterProperty="Foreground"Value="#3190FF"/><SetterProperty="Background"Value="#3190FF"/><SetterProperty="BorderBrush"Value="#3190FF"/></Trigger><!-- 鼠标悬停状态 --><TriggerProperty="IsMouseOver"Value="True"><SetterProperty="IsEnabled"Value="True"/><!--<SetterProperty="Background"Value="#FF5252"/>--></Trigger></Style.Triggers></Style></ResourceDictionary></Grid.Resources><!--Command="{Binding CheckedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"--><!--Background="#00D918"--><ToggleButton x:Name="ToggleButton1"HorizontalAlignment="Center"Checked="ToggleButton_Checked"IsChecked="{Binding IsChecked, Mode=Default, RelativeSource={RelativeSource AncestorType=UserControl}, UpdateSourceTrigger=Explicit}"PreviewMouseWheel="ToggleButton_PreviewMouseWheel"PreviewMouseLeftButtonDown="ToggleButton1_PreviewMouseLeftButtonDown"Style="{StaticResource CustomToggleButton}"Unchecked="ToggleButton_Unchecked"VirtualizingPanel.IsVirtualizing="False"VirtualizingPanel.VirtualizationMode="Standard"/></Grid></UserControl>
usingCommon.Model.ZhiYeBingTiJian;usingFastReport.Data;usingGalaSoft.MvvmLight;usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Controls.Primitives;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceModule.PEIS.ZhiYeBingTiJian.View{/// <summary>/// HandyControl主题样式,滑动开关控件 ,/// ToggleButtonCustuom.xaml 的交互逻辑/// </summary>/// <remarks>/// HandyControl主题样式,滑动开关控件 ,/// 创建时间:2025-11-27 11:11:15,作者:王浩力/// </remarks>publicpartialclassToggleButtonCustuom:UserControl{publicToggleButtonCustuom(){InitializeComponent();//this.DataContext = this;}// 注册依赖属性publicstaticreadonlyDependencyPropertyCustomMyProperty=DependencyProperty.Register("IsChecked",typeof(bool),typeof(ToggleButtonCustuom),newPropertyMetadata(true));/// <summary>/// 获取或设置IsChecked,true开启,false关闭/// </summary>publicboolIsChecked{get=>(bool)GetValue(CustomMyProperty);set=>SetValue(CustomMyProperty,value);}/// <summary>/// 定义自定义事件,当IsChecked改变时触发/// </summary>publiceventRoutedEventHandlerOnChecked;System.Threading.Timer_timer;ToggleButtonbutton;boololdValue=false;privatevoidToggleButton_Checked(objectsender,RoutedEventArgse){//// 过滤非用户交互的触发:检查鼠标/键盘状态//if (Mouse.LeftButton != MouseButtonState.Pressed && !Keyboard.IsKeyDown(Key.Space))//{// e.Handled = true; // 标记事件已处理,终止传递// return;//}if(!_isManualClick){_isManualClick=false;// 重置标志return;}OnChecked?.Invoke(sender,e);// 执行绑定的命令//CheckedCommand?.Execute(e );//CheckedCommand?.Execute(CommandParameter ?? e );CheckedCommand?.Execute(newobject[]{e,CommandParameter,this});button=senderasToggleButton;oldValue=button.IsChecked.Value;_isManualClick=false;// 重置标志}privatevoidToggleButton_Unchecked(objectsender,RoutedEventArgse){// 过滤非用户交互的触发:检查鼠标/键盘状态//if (Mouse.LeftButton != MouseButtonState.Pressed && !Keyboard.IsKeyDown(Key.Space))//{// e.Handled = true; // 标记事件已处理,终止传递// return;//}if(!_isManualClick){_isManualClick=false;// 重置标志return;}OnChecked?.Invoke(sender,e);// 执行绑定的命令//CheckedCommand?.Execute(e );//CheckedCommand?.Execute(CommandParameter ?? e );CheckedCommand?.Execute(newobject[]{e,CommandParameter,this});button=senderasToggleButton;oldValue=button.IsChecked.Value;_isManualClick=false;// 重置标志}DateTimelastTriggerTime2=DateTime.MinValue;/// <summary>/// 修改数据失败时,调用重置状态为原始数据状态/// </summary>publicvoidFailResetChecked(){varspan=(DateTime.Now-lastTriggerTime2).TotalMilliseconds;Debug.WriteLine("间隔时间毫秒:"+span);if(span<1700)//防止重复触发{return;}lastTriggerTime2=DateTime.Now;if(button==null){return;}// 800毫秒后重置为不可修改状态_timer=newSystem.Threading.Timer(callback:(a)=>{Dispatcher.Invoke(()=>{button.IsChecked=!oldValue;});},// 回调方法state:null,dueTime:800,// 首次执行前的延迟(1500毫秒)period:-1// 后续间隔设为-1(仅执行一次));}// 定义命令依赖属性publicstaticreadonlyDependencyPropertyCheckedCommandProperty=DependencyProperty.Register("CheckedCommand",typeof(ICommand),typeof(ToggleButtonCustuom),newPropertyMetadata(null));/// <summary>/// 暴露一个 ICommand属性,改变值时触发命令/// </summary>publicICommandCheckedCommand{get{return(ICommand)GetValue(CheckedCommandProperty);}set{SetValue(CheckedCommandProperty,value);}}publicobjectCommandParameter{get=>GetValue(CommandParameterProperty);set=>SetValue(CommandParameterProperty,value);}publicstaticreadonlyDependencyPropertyCommandParameterProperty=DependencyProperty.Register("CommandParameter",typeof(object),typeof(ToggleButtonCustuom));/// <summary>/// // 阻止滚轮事件传递,禁用滚轮改变IsChecked的行为/// </summary>/// <param name="sender"></param>/// <param name="e"></param>privatevoidToggleButton_PreviewMouseWheel(objectsender,MouseWheelEventArgse){e.Handled=true;// 标记事件已处理,阻止默认行为}/// <summary>/// 标记是否为手动点击触发/// </summary>privatebool_isManualClick=false;privatevoidToggleButton1_PreviewMouseLeftButtonDown(objectsender,MouseButtonEventArgse){_isManualClick=true;e.Handled=false;// 允许事件继续传递}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/15 5:47:27

基于SpringBoot的社区技术交流平台系统(源码+lw+部署文档+讲解等)

课题介绍随着信息技术快速发展&#xff0c;社区内技术爱好者、从业者的交流需求日益增长&#xff0c;但当前社区技术交流存在信息分散、交流渠道单一、优质资源难以沉淀、技术问题解决效率低等问题&#xff0c;制约了社区技术氛围的营造与成员技术能力的提升。本课题以搭建高效…

作者头像 李华
网站建设 2026/1/18 8:22:55

让数据类型回归语义:ABAP CDS 的 Type 与 Enum 在 ABAP Cloud 里的实战指南

在很多 ABAP 项目里,数据模型的语义经常被迫拆散到不同的地方:技术类型在 Domain,业务含义在 Data Element,固定值在 Domain 固定值,界面标题和字段提示又靠一堆维护文本来兜底。放在经典 ABAP On-Premise 时代,这套体系非常成熟;但一旦你开始做 ABAP Cloud、RAP、CDS V…

作者头像 李华
网站建设 2026/1/17 5:31:15

AWS推出AI图像编辑新突破:用说话就能精准移动图片中的物体!

这项来自香港中文大学、AWS智能AI部门、亚马逊云服务和亚马逊机器人团队的联合研究发表于2025年1月&#xff0c;论文编号为arXiv:2601.02356v1。研究团队由谭靖、张兆阳、沈彦涛、蔡嘉瑞等多位学者组成&#xff0c;有兴趣深入了解的读者可以通过该编号查询完整论文。想要修改照…

作者头像 李华
网站建设 2026/1/15 6:37:17

从案例到技巧:Agentic AI提示设计的实战总结(提示工程架构师版)

好的&#xff0c;技术架构师&#xff01;基于您提供的主题&#xff0c;我为您精心构思一篇面向**具备基础提示工程知识、致力于构建复杂可靠Agent系统的高级用户&#xff08;如提示工程架构师、技术负责人、高级AI工程师&#xff09;**的实战深度总结文章。文章将聚焦可落地的设…

作者头像 李华
网站建设 2026/1/20 11:21:30

光谱共焦技术在高精度尺寸与3D表面缺陷检测中的工业应用研究

摘要&#xff1a;随着智能制造与精密工业的快速发展&#xff0c;对非接触、高精度、高速度的在线检测技术需求日益迫切。以海伯森技术推出的系列高端光学传感器深入剖析其基于光谱共焦位移测量与光谱共焦成像的核心原理。重点阐述该技术如何在微观尺度上实现纳米级精度的三维尺…

作者头像 李华
网站建设 2026/1/20 16:43:13

GDAL 实现矢量裁剪

前言 ❝ 矢量数据作为数据处理的半壁江山&#xff0c;在日常工作中涉及到多种操作&#xff0c;矢量数据裁剪尤其具有代表性和重要性&#xff0c;是常用操作&#xff0c;核心原理为从指定数据中提取出目标范围。在之前的文章中讲了如何使用GDAL或者ogr2ogr工具将txt以及csv文本数…

作者头像 李华