优化(
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<UserControl x:Class="Ink_Canvas.Controls.PenColorButton"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
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="45" d:DesignWidth="45">
|
||||
<Border x:Name="ButtonBorder"
|
||||
BorderThickness="1.5"
|
||||
CornerRadius="100"
|
||||
Width="45" Height="45"
|
||||
MouseUp="ButtonBorder_MouseUp">
|
||||
<Canvas x:Name="RootCanvas">
|
||||
<Image x:Name="TransparentGridImage"
|
||||
Source="/Resources/Icons-png/transparent-grid.png"
|
||||
Width="42" Height="42"
|
||||
Visibility="Collapsed">
|
||||
<Image.Clip>
|
||||
<EllipseGeometry x:Name="ClipGeometry"
|
||||
Center="21,21"
|
||||
RadiusX="21"
|
||||
RadiusY="21" />
|
||||
</Image.Clip>
|
||||
</Image>
|
||||
<Border x:Name="ColorBorder"
|
||||
Width="42" Height="42"
|
||||
CornerRadius="21"
|
||||
Opacity="1" />
|
||||
<Viewbox x:Name="CheckViewbox"
|
||||
Visibility="Collapsed"
|
||||
Margin="8"
|
||||
Canvas.Top="0"
|
||||
Canvas.Left="0">
|
||||
<Image x:Name="CheckImage"
|
||||
RenderOptions.BitmapScalingMode="HighQuality"
|
||||
VerticalAlignment="Top"
|
||||
Width="24" Height="24" />
|
||||
</Viewbox>
|
||||
</Canvas>
|
||||
</Border>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,141 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Ink_Canvas.Controls
|
||||
{
|
||||
public partial class PenColorButton : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
|
||||
nameof(Color), typeof(Color), typeof(PenColorButton),
|
||||
new PropertyMetadata(Colors.Black, OnColorChanged));
|
||||
|
||||
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (PenColorButton)d;
|
||||
if (button.ColorBorder != null)
|
||||
{
|
||||
button.ColorBorder.Background = new SolidColorBrush((Color)e.NewValue);
|
||||
}
|
||||
}
|
||||
|
||||
public Color Color
|
||||
{
|
||||
get => (Color)GetValue(ColorProperty);
|
||||
set => SetValue(ColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty BorderBrushColorProperty = DependencyProperty.Register(
|
||||
nameof(BorderBrushColor), typeof(Color), typeof(PenColorButton),
|
||||
new PropertyMetadata(Colors.Gray, OnBorderBrushColorChanged));
|
||||
|
||||
private static void OnBorderBrushColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (PenColorButton)d;
|
||||
if (button.ButtonBorder != null)
|
||||
{
|
||||
button.ButtonBorder.BorderBrush = new SolidColorBrush((Color)e.NewValue);
|
||||
}
|
||||
}
|
||||
|
||||
public Color BorderBrushColor
|
||||
{
|
||||
get => (Color)GetValue(BorderBrushColorProperty);
|
||||
set => SetValue(BorderBrushColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty IsHighlighterProperty = DependencyProperty.Register(
|
||||
nameof(IsHighlighter), typeof(bool), typeof(PenColorButton),
|
||||
new PropertyMetadata(false, OnIsHighlighterChanged));
|
||||
|
||||
private static void OnIsHighlighterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (PenColorButton)d;
|
||||
if (button.TransparentGridImage != null && button.ColorBorder != null)
|
||||
{
|
||||
bool isHighlighter = (bool)e.NewValue;
|
||||
button.TransparentGridImage.Visibility = isHighlighter ? Visibility.Visible : Visibility.Collapsed;
|
||||
button.ColorBorder.Opacity = isHighlighter ? 0.75 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsHighlighter
|
||||
{
|
||||
get => (bool)GetValue(IsHighlighterProperty);
|
||||
set => SetValue(IsHighlighterProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
|
||||
nameof(IsChecked), typeof(bool), typeof(PenColorButton),
|
||||
new PropertyMetadata(false, OnIsCheckedChanged));
|
||||
|
||||
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (PenColorButton)d;
|
||||
if (button.CheckViewbox != null)
|
||||
{
|
||||
button.CheckViewbox.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsChecked
|
||||
{
|
||||
get => (bool)GetValue(IsCheckedProperty);
|
||||
set => SetValue(IsCheckedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CheckIconSourceProperty = DependencyProperty.Register(
|
||||
nameof(CheckIconSource), typeof(string), typeof(PenColorButton),
|
||||
new PropertyMetadata("/Resources/new-icons/checked-white.png", OnCheckIconSourceChanged));
|
||||
|
||||
private static void OnCheckIconSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (PenColorButton)d;
|
||||
if (button.CheckImage != null && e.NewValue is string source)
|
||||
{
|
||||
button.CheckImage.Source = new System.Windows.Media.Imaging.BitmapImage(new System.Uri(source, System.UriKind.Relative));
|
||||
}
|
||||
}
|
||||
|
||||
public string CheckIconSource
|
||||
{
|
||||
get => (string)GetValue(CheckIconSourceProperty);
|
||||
set => SetValue(CheckIconSourceProperty, value);
|
||||
}
|
||||
|
||||
public event MouseButtonEventHandler ButtonMouseUp;
|
||||
|
||||
public PenColorButton()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += PenColorButton_Loaded;
|
||||
}
|
||||
|
||||
private void PenColorButton_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ApplyProperties();
|
||||
}
|
||||
|
||||
private void ApplyProperties()
|
||||
{
|
||||
if (ColorBorder != null)
|
||||
ColorBorder.Background = new SolidColorBrush(Color);
|
||||
if (ButtonBorder != null)
|
||||
ButtonBorder.BorderBrush = new SolidColorBrush(BorderBrushColor);
|
||||
if (TransparentGridImage != null)
|
||||
TransparentGridImage.Visibility = IsHighlighter ? Visibility.Visible : Visibility.Collapsed;
|
||||
if (ColorBorder != null)
|
||||
ColorBorder.Opacity = IsHighlighter ? 0.75 : 1;
|
||||
if (CheckViewbox != null)
|
||||
CheckViewbox.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed;
|
||||
if (CheckImage != null && !string.IsNullOrEmpty(CheckIconSource))
|
||||
CheckImage.Source = new System.Windows.Media.Imaging.BitmapImage(new System.Uri(CheckIconSource, System.UriKind.Relative));
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ButtonMouseUp?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user