From f7befe387c5e572eff83b7bef77426d1a268a8cc Mon Sep 17 00:00:00 2001 From: PrefacedCorg <1876568293@qq.com> Date: Fri, 10 Apr 2026 22:59:22 +0800 Subject: [PATCH] =?UTF-8?q?add:=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8E=A7?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Controls/LabeledToggleSwitch.xaml | 26 +++++ .../Controls/LabeledToggleSwitch.xaml.cs | 96 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 Ink Canvas/Controls/LabeledToggleSwitch.xaml create mode 100644 Ink Canvas/Controls/LabeledToggleSwitch.xaml.cs diff --git a/Ink Canvas/Controls/LabeledToggleSwitch.xaml b/Ink Canvas/Controls/LabeledToggleSwitch.xaml new file mode 100644 index 00000000..369d40a8 --- /dev/null +++ b/Ink Canvas/Controls/LabeledToggleSwitch.xaml @@ -0,0 +1,26 @@ + + + + + + + + + \ No newline at end of file diff --git a/Ink Canvas/Controls/LabeledToggleSwitch.xaml.cs b/Ink Canvas/Controls/LabeledToggleSwitch.xaml.cs new file mode 100644 index 00000000..668e15cd --- /dev/null +++ b/Ink Canvas/Controls/LabeledToggleSwitch.xaml.cs @@ -0,0 +1,96 @@ +using System.Windows; +using System.Windows.Controls; +using ui = iNKORE.UI.WPF.Modern.Controls; + +namespace Ink_Canvas.Controls +{ + public partial class LabeledToggleSwitch : UserControl + { + public ui.ToggleSwitch ToggleSwitchControl => ToggleSwitch; + + public static readonly DependencyProperty LabelProperty = DependencyProperty.Register( + nameof(Label), typeof(string), typeof(LabeledToggleSwitch), new PropertyMetadata(string.Empty)); + + public string Label + { + get => (string)GetValue(LabelProperty); + set => SetValue(LabelProperty, value); + } + + public static readonly DependencyProperty HintProperty = DependencyProperty.Register( + nameof(Hint), typeof(string), typeof(LabeledToggleSwitch), new PropertyMetadata(string.Empty, OnHintChanged)); + + public string Hint + { + get => (string)GetValue(HintProperty); + set => SetValue(HintProperty, value); + } + + private static void OnHintChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is LabeledToggleSwitch control) + { + var hint = e.NewValue as string; + control.HintTextBlock.Visibility = string.IsNullOrEmpty(hint) ? Visibility.Collapsed : Visibility.Visible; + } + } + + public static readonly DependencyProperty IsOnProperty = DependencyProperty.Register( + nameof(IsOn), typeof(bool), typeof(LabeledToggleSwitch), new PropertyMetadata(false)); + + public bool IsOn + { + get => (bool)GetValue(IsOnProperty); + set => SetValue(IsOnProperty, value); + } + + public static readonly DependencyProperty OnContentProperty = DependencyProperty.Register( + nameof(OnContent), typeof(string), typeof(LabeledToggleSwitch), new PropertyMetadata(string.Empty)); + + public string OnContent + { + get => (string)GetValue(OnContentProperty); + set => SetValue(OnContentProperty, value); + } + + public static readonly DependencyProperty OffContentProperty = DependencyProperty.Register( + nameof(OffContent), typeof(string), typeof(LabeledToggleSwitch), new PropertyMetadata(string.Empty)); + + public string OffContent + { + get => (string)GetValue(OffContentProperty); + set => SetValue(OffContentProperty, value); + } + + public static readonly DependencyProperty ShowWhenProperty = DependencyProperty.Register( + nameof(ShowWhen), typeof(bool), typeof(LabeledToggleSwitch), new PropertyMetadata(true, OnShowWhenChanged)); + + public bool ShowWhen + { + get => (bool)GetValue(ShowWhenProperty); + set => SetValue(ShowWhenProperty, value); + } + + private static void OnShowWhenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is LabeledToggleSwitch control) + { + control.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed; + } + } + + public event RoutedEventHandler Toggled; + + public LabeledToggleSwitch() + { + InitializeComponent(); + Visibility = ShowWhen ? Visibility.Visible : Visibility.Collapsed; + HintTextBlock.Visibility = string.IsNullOrEmpty(Hint) ? Visibility.Collapsed : Visibility.Visible; + } + + private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e) + { + Toggled?.Invoke(this, e); + } + } +}