当前位置: 首页 > news >正文

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;// 允许事件继续传递}}}
http://www.jsqmd.com/news/226314/

相关文章:

  • archlinux 如何调整 笔记本内置屏幕的亮度
  • 阳明交通大学突破:动态视频重建技术实现画质动作双优化
  • 救命神器8个AI论文软件,助你轻松搞定本科毕业论文!
  • 伯克利团队破解AI评测难题:让机器学会自动出题的神奇方法
  • 一键生成AI播客
  • 腾讯优图Youtu-Agent:AI代理实现自动化生成突破
  • 构建个人知识库工具分类与对比
  • 2026必备!继续教育必看!10款一键生成论文工具深度测评
  • 商汤突破:全能AI助手集成搜索识图与自主思考
  • 中药材原料口碑排行榜:哪些药材最受欢迎?
  • 交通仿真软件:Paramics_(6).交通控制策略仿真
  • KAIST团队突破虚拟对话新纪元:让AI头像像真人一样自然互动
  • 亲测好用8个AI论文软件,本科生搞定毕业论文不求人!
  • 亲测好用8个AI论文软件,本科生搞定毕业论文不求人!
  • 清华大学团队突破AI视频理解难题:用“反常识“训练让机器看懂真相
  • 武汉市放飞炬人产业引导基金:将起草 房地产转让工业信托基金 合同草书
  • 剑桥大学最新突破:让AI既聪明又富有创造力的秘诀
  • python中各种数据类型的转换方法
  • 腾讯天美AI团队重新定义语言模型训练:精确还是多样?
  • PX4实战(十一):PX4运动规划模块(flight mode manager)详解
  • leetcode热题括号生成
  • 雷家林(レイ・ジアリン)詩歌集録 その十四(日译版)
  • 让数据类型回归语义:ABAP CDS 的 Type 与 Enum 在 ABAP Cloud 里的实战指南
  • 香港科技大学突破AI画图“作弊“难题:让机器学会诚实创作
  • SSE、长轮询与 WebSocket 连接资源对比及 Spring Boot 配置指南
  • AWS推出AI图像编辑新突破:用说话就能精准移动图片中的物体!
  • 雷家林(レイ・ジアリン)詩歌集録 その十五(日译版)
  • 让 RAP 弹窗参数真正变成必填:Excel Upload Action 的两种实现路径
  • 《呼吸秋千》我早已习惯,饥饿在呼吸上荡起秋千
  • 2026必备!专科生毕业论文TOP8 AI论文网站测评