add:自定义控件

This commit is contained in:
PrefacedCorg
2026-04-10 22:59:22 +08:00
parent b1a34b7a64
commit f7befe387c
2 changed files with 122 additions and 0 deletions
@@ -0,0 +1,26 @@
<UserControl x:Class="Ink_Canvas.Controls.LabeledToggleSwitch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf">
<ikw:SimpleStackPanel Spacing="4">
<ikw:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Text="{Binding Label, RelativeSource={RelativeSource AncestorType=UserControl}}"
VerticalAlignment="Center"
Foreground="#fafafa"
FontSize="14"
Margin="0,0,16,0"/>
<ui:ToggleSwitch x:Name="ToggleSwitch"
OnContent="{Binding OnContent, RelativeSource={RelativeSource AncestorType=UserControl}}"
OffContent="{Binding OffContent, RelativeSource={RelativeSource AncestorType=UserControl}}"
IsOn="{Binding IsOn, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=TwoWay}"
FontWeight="Bold"
Toggled="ToggleSwitch_Toggled"/>
</ikw:SimpleStackPanel>
<TextBlock x:Name="HintTextBlock"
Text="{Binding Hint, RelativeSource={RelativeSource AncestorType=UserControl}}"
TextWrapping="Wrap"
Foreground="#a1a1aa"
Visibility="Collapsed"/>
</ikw:SimpleStackPanel>
</UserControl>
@@ -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);
}
}
}