news 2026/8/29 1:07:37

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/8/21 18:46:16

基于SpringBoot的人力资源管理系统(源码+lw+部署文档+讲解等)

课题介绍随着企业规模扩大与数字化转型推进&#xff0c;人力资源管理工作复杂度不断提升&#xff0c;但当前多数企业存在员工信息管理分散、招聘流程不规范、考勤绩效核算低效、培训发展体系不完善等问题&#xff0c;制约了人力资源管理效率与企业核心竞争力的提升。本课题以搭…

作者头像 李华
网站建设 2026/8/23 0:06:29

基于SpringBoot的社区家政管理系统(源码+lw+部署文档+讲解等)

课题介绍随着人口老龄化加剧及居民生活品质提升&#xff0c;社区家政服务需求日益增长&#xff0c;但当前社区家政服务普遍存在服务资源分散、家政人员资质难核验、服务流程不规范、订单管理低效、服务质量难保障等问题&#xff0c;制约了家政服务效率与居民满意度的提升。本课…

作者头像 李华
网站建设 2026/8/24 23:44:04

基于SpringBoot的社区老年人健康管理系统(源码+lw+部署文档+讲解等)

课题介绍随着人口老龄化进程加快&#xff0c;社区老年人健康管理需求日益迫切&#xff0c;但当前社区老年人健康管理普遍存在健康档案不健全、健康监测不及时、慢病管理不规范、健康服务资源分散、家属沟通协同不足等问题&#xff0c;制约了健康管理服务质量与老年人生活质量的…

作者头像 李华
网站建设 2026/8/27 16:31:13

读懂 sap.m.URLHelper 文档里的 library: sap.m 与 Module: sap/m/library

你在 SAP UI5 API Reference 里看到的这两个字段&#xff0c;看起来都在讲 sap.m&#xff0c;但它们回答的是两类完全不同的问题&#xff1a; library: sap.m&#xff1a;这条 API 属于哪个 UI5 库&#xff08;library 维度&#xff1a;交付、依赖、主题、预加载等的组织单位&…

作者头像 李华
网站建设 2026/8/25 19:41:24

基于SpringBoot的考编论坛网站毕业设计源码

博主介绍&#xff1a;✌ 专注于Java,python,✌关注✌私信我✌具体的问题&#xff0c;我会尽力帮助你。一、研究目的本研究旨在构建一个基于SpringBoot框架的考编论坛网站&#xff0c;以实现以下研究目的&#xff1a; 首先&#xff0c;通过设计并实现一个功能完善的考编论坛网站…

作者头像 李华