< ! -- 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> using Common. Model. ZhiYeBingTiJian ; using FastReport. Data ; using GalaSoft. MvvmLight ; using System ; using System. Collections. Generic ; using System. Diagnostics ; using System. Linq ; using System. Text ; using System. Threading. Tasks ; using System. Windows ; using System. Windows. Controls ; using System. Windows. Controls. Primitives ; using System. Windows. Data ; using System. Windows. Documents ; using System. Windows. Input ; using System. Windows. Media ; using System. Windows. Media. Imaging ; using System. Windows. Navigation ; using System. Windows. Shapes ; namespace Module. PEIS. ZhiYeBingTiJian. View { /// <summary> /// HandyControl主题样式,滑动开关控件 , /// ToggleButtonCustuom.xaml 的交互逻辑 /// </summary> /// <remarks> /// HandyControl主题样式,滑动开关控件 , /// 创建时间:2025-11-27 11:11:15,作者:王浩力 /// </remarks> public partial class ToggleButtonCustuom : UserControl { public ToggleButtonCustuom ( ) { InitializeComponent ( ) ; //this.DataContext = this; } // 注册依赖属性 public static readonly DependencyProperty CustomMyProperty= DependencyProperty. Register ( "IsChecked" , typeof ( bool ) , typeof ( ToggleButtonCustuom ) , new PropertyMetadata ( true ) ) ; /// <summary> /// 获取或设置IsChecked,true开启,false关闭 /// </summary> public bool IsChecked{ get => ( bool ) GetValue ( CustomMyProperty) ; set => SetValue ( CustomMyProperty, value ) ; } /// <summary> /// 定义自定义事件,当IsChecked改变时触发 /// </summary> public event RoutedEventHandler OnChecked; System. Threading. Timer _timer; ToggleButton button; bool oldValue= false ; private void ToggleButton_Checked ( object sender, RoutedEventArgs e) { //// 过滤非用户交互的触发:检查鼠标/键盘状态 //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 ( new object [ ] { e, CommandParameter, this } ) ; button= senderas ToggleButton ; oldValue= button. IsChecked. Value; _isManualClick= false ; // 重置标志 } private void ToggleButton_Unchecked ( object sender, RoutedEventArgs e) { // 过滤非用户交互的触发:检查鼠标/键盘状态 //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 ( new object [ ] { e, CommandParameter, this } ) ; button= senderas ToggleButton ; oldValue= button. IsChecked. Value; _isManualClick= false ; // 重置标志 } DateTime lastTriggerTime2= DateTime. MinValue; /// <summary> /// 修改数据失败时,调用重置状态为原始数据状态 /// </summary> public void FailResetChecked ( ) { var span= ( DateTime. Now- lastTriggerTime2) . TotalMilliseconds; Debug. WriteLine ( "间隔时间毫秒:" + span) ; if ( span< 1700 ) //防止重复触发 { return ; } lastTriggerTime2= DateTime. Now; if ( button== null ) { return ; } // 800毫秒后重置为不可修改状态 _timer= new System. Threading. Timer ( callback : ( a) => { Dispatcher. Invoke ( ( ) => { button. IsChecked= ! oldValue; } ) ; } , // 回调方法 state: null , dueTime : 800 , // 首次执行前的延迟(1500毫秒) period: - 1 // 后续间隔设为-1(仅执行一次) ) ; } // 定义命令依赖属性 public static readonly DependencyProperty CheckedCommandProperty= DependencyProperty. Register ( "CheckedCommand" , typeof ( ICommand ) , typeof ( ToggleButtonCustuom ) , new PropertyMetadata ( null ) ) ; /// <summary> /// 暴露一个 ICommand属性,改变值时触发命令 /// </summary> public ICommand CheckedCommand{ get { return ( ICommand) GetValue ( CheckedCommandProperty) ; } set { SetValue ( CheckedCommandProperty, value ) ; } } public object CommandParameter{ get => GetValue ( CommandParameterProperty) ; set => SetValue ( CommandParameterProperty, value ) ; } public static readonly DependencyProperty CommandParameterProperty= DependencyProperty. Register ( "CommandParameter" , typeof ( object ) , typeof ( ToggleButtonCustuom ) ) ; /// <summary> /// // 阻止滚轮事件传递,禁用滚轮改变IsChecked的行为 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToggleButton_PreviewMouseWheel ( object sender, MouseWheelEventArgs e) { e. Handled= true ; // 标记事件已处理,阻止默认行为 } /// <summary> /// 标记是否为手动点击触发 /// </summary> private bool _isManualClick= false ; private void ToggleButton1_PreviewMouseLeftButtonDown ( object sender, MouseButtonEventArgs e) { _isManualClick= true ; e. Handled= false ; // 允许事件继续传递 } } }