最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wpf自定义checkbox开关控件样式例子
时间:2022-06-25 08:43:16 编辑:袖梨 来源:一聚教程网
xaml代码:
代码如下 | 复制代码 |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="24" d:DesignWidth="60"> Width="60" Height="24"> Padding="0" CornerRadius="8" Background="#2ecc71"> Visibility="Collapsed"/> BorderThickness="1" HorizontalAlignment="Left" CornerRadius="15" Width="24" Height="24"> StrokeThickness="0" Width="22" RadiusX="15" RadiusY="15"/> |
Cs代码:主要是重构一http://www.111com.net些事件和返回参数:
代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; 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 Kaitone.DetectiveHelper.UI.Controls { /// /// ToggleSwitchButton.xaml 的交互逻辑 /// public partial class ToggleSwitchButton : UserControl { public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(ToggleSwitchButton), new PropertyMetadata(default(bool), OnIsCheckedChanged)); public event RoutedEventHandler Checked; public event RoutedEventHandler UnChecked; public bool IsChecked { get { return (bool)GetValue(IsCheckedProperty); } set { SetValue(IsCheckedProperty, value); } } public ToggleSwitchButton() { InitializeComponent(); } private static void OnIsCheckedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { (obj as ToggleSwitchButton).OnIsCheckedChanged(args); } private void OnIsCheckedChanged(DependencyPropertyChangedEventArgs args) { fillRectangle.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed; slideBorder.HorizontalAlignment = IsChecked ? HorizontalAlignment.Right : HorizontalAlignment.Left; if (IsChecked && Checked != null) { Checked(this, new RoutedEventArgs()); } if (!IsChecked && UnChecked != null) { UnChecked(this, new RoutedEventArgs()); } } protected override void OnMouseLeftButtonUp(MouseButtonEventArgs args) { args.Handled = true; IsChecked ^= true; base.OnMouseLeftButtonUp(args); } } } |
相关文章
- 《原神》5.2卡池抽取建议 11-14
- 《原神》5.2版本新怪物介绍 11-14
- 《原神》希诺宁增伤触发方法 11-14
- 《原神》循音觅奇活动入口 11-14
- 《原神》循音觅奇兑换码获取方法 11-14
- 《原神》花羽会活动飞行技巧介绍 11-14