优化
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<UserControl x:Class="Ink_Canvas.Controls.CircularColorButton"
|
||||
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"
|
||||
BorderBrush="#fed7aa"
|
||||
BorderThickness="1.5"
|
||||
CornerRadius="100"
|
||||
MouseDown="ButtonBorder_MouseDown"
|
||||
MouseLeave="ButtonBorder_MouseLeave"
|
||||
MouseUp="ButtonBorder_MouseUp">
|
||||
<Canvas x:Name="ButtonCanvas">
|
||||
<Image x:Name="TransparentGridImage"
|
||||
Source="/Resources/Icons-png/transparent-grid.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality">
|
||||
<Image.Clip>
|
||||
<EllipseGeometry x:Name="ImageClipGeometry" Center="21,21" RadiusX="21" RadiusY="21"/>
|
||||
</Image.Clip>
|
||||
</Image>
|
||||
<Border x:Name="ColorOverlay"
|
||||
CornerRadius="21"
|
||||
Opacity="0.75"/>
|
||||
<Viewbox x:Name="CheckViewbox"
|
||||
Visibility="Visible"
|
||||
Canvas.Top="9"
|
||||
Canvas.Left="9"
|
||||
Width="24"
|
||||
Height="24">
|
||||
<Image x:Name="CheckImage"
|
||||
Source="/Resources/new-icons/checked-white.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality"
|
||||
VerticalAlignment="Top"/>
|
||||
</Viewbox>
|
||||
</Canvas>
|
||||
</Border>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Ink_Canvas.Controls
|
||||
{
|
||||
public partial class CircularColorButton : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
|
||||
nameof(Color), typeof(Color), typeof(CircularColorButton),
|
||||
new PropertyMetadata(Colors.Transparent, OnColorChanged));
|
||||
|
||||
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)d;
|
||||
if (button.ColorOverlay != null)
|
||||
button.ColorOverlay.Background = new SolidColorBrush((Color)e.NewValue);
|
||||
}
|
||||
|
||||
public Color Color
|
||||
{
|
||||
get => (Color)GetValue(ColorProperty);
|
||||
set => SetValue(ColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ColorOpacityProperty = DependencyProperty.Register(
|
||||
nameof(ColorOpacity), typeof(double), typeof(CircularColorButton),
|
||||
new PropertyMetadata(0.75, OnColorOpacityChanged));
|
||||
|
||||
private static void OnColorOpacityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)d;
|
||||
if (button.ColorOverlay != null)
|
||||
button.ColorOverlay.Opacity = (double)e.NewValue;
|
||||
}
|
||||
|
||||
public double ColorOpacity
|
||||
{
|
||||
get => (double)GetValue(ColorOpacityProperty);
|
||||
set => SetValue(ColorOpacityProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
|
||||
nameof(IsChecked), typeof(bool), typeof(CircularColorButton),
|
||||
new PropertyMetadata(true, OnIsCheckedChanged));
|
||||
|
||||
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)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 ButtonSizeProperty = DependencyProperty.Register(
|
||||
nameof(ButtonSize), typeof(double), typeof(CircularColorButton),
|
||||
new PropertyMetadata(45.0, OnButtonSizeChanged));
|
||||
|
||||
private static void OnButtonSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)d;
|
||||
if (button.ButtonBorder == null) return;
|
||||
|
||||
var size = (double)e.NewValue;
|
||||
button.ButtonBorder.Width = size;
|
||||
button.ButtonBorder.Height = size;
|
||||
var radius = (size - 3) / 2;
|
||||
button.ColorOverlay.Width = radius * 2;
|
||||
button.ColorOverlay.Height = radius * 2;
|
||||
button.ImageClipGeometry.RadiusX = radius;
|
||||
button.ImageClipGeometry.RadiusY = radius;
|
||||
button.ImageClipGeometry.Center = new Point(radius, radius);
|
||||
button.TransparentGridImage.Width = radius * 2;
|
||||
button.TransparentGridImage.Height = radius * 2;
|
||||
}
|
||||
|
||||
public double ButtonSize
|
||||
{
|
||||
get => (double)GetValue(ButtonSizeProperty);
|
||||
set => SetValue(ButtonSizeProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty BorderBrushColorProperty = DependencyProperty.Register(
|
||||
nameof(BorderBrushColor), typeof(Color), typeof(CircularColorButton),
|
||||
new PropertyMetadata(Color.FromRgb(254, 215, 170), OnBorderBrushColorChanged));
|
||||
|
||||
private static void OnBorderBrushColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)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 CheckIconSourceProperty = DependencyProperty.Register(
|
||||
nameof(CheckIconSource), typeof(string), typeof(CircularColorButton),
|
||||
new PropertyMetadata("/Resources/new-icons/checked-white.png", OnCheckIconSourceChanged));
|
||||
|
||||
private static void OnCheckIconSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (CircularColorButton)d;
|
||||
if (button.CheckImage != null)
|
||||
button.CheckImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri((string)e.NewValue, UriKind.Relative));
|
||||
}
|
||||
|
||||
public string CheckIconSource
|
||||
{
|
||||
get => (string)GetValue(CheckIconSourceProperty);
|
||||
set => SetValue(CheckIconSourceProperty, value);
|
||||
}
|
||||
|
||||
public event MouseButtonEventHandler ButtonMouseDown;
|
||||
public event MouseEventHandler ButtonMouseLeave;
|
||||
public event RoutedEventHandler ButtonMouseUp;
|
||||
|
||||
public CircularColorButton()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += CircularColorButton_Loaded;
|
||||
}
|
||||
|
||||
private void CircularColorButton_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ApplyAllProperties();
|
||||
}
|
||||
|
||||
private void ApplyAllProperties()
|
||||
{
|
||||
if (ButtonBorder != null)
|
||||
{
|
||||
ButtonBorder.BorderBrush = new SolidColorBrush(BorderBrushColor);
|
||||
var size = ButtonSize;
|
||||
ButtonBorder.Width = size;
|
||||
ButtonBorder.Height = size;
|
||||
}
|
||||
if (ColorOverlay != null)
|
||||
{
|
||||
ColorOverlay.Background = new SolidColorBrush(Color);
|
||||
ColorOverlay.Opacity = ColorOpacity;
|
||||
var radius = (ButtonSize - 3) / 2;
|
||||
ColorOverlay.Width = radius * 2;
|
||||
ColorOverlay.Height = radius * 2;
|
||||
}
|
||||
if (ImageClipGeometry != null)
|
||||
{
|
||||
var radius = (ButtonSize - 3) / 2;
|
||||
ImageClipGeometry.RadiusX = radius;
|
||||
ImageClipGeometry.RadiusY = radius;
|
||||
ImageClipGeometry.Center = new Point(radius, radius);
|
||||
}
|
||||
if (TransparentGridImage != null)
|
||||
{
|
||||
var radius = (ButtonSize - 3) / 2;
|
||||
TransparentGridImage.Width = radius * 2;
|
||||
TransparentGridImage.Height = radius * 2;
|
||||
}
|
||||
if (CheckViewbox != null)
|
||||
{
|
||||
CheckViewbox.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
if (CheckImage != null)
|
||||
{
|
||||
CheckImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(CheckIconSource, UriKind.Relative));
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ButtonMouseDown?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseLeave(object sender, MouseEventArgs e)
|
||||
{
|
||||
ButtonMouseLeave?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ButtonMouseUp?.Invoke(this, new RoutedEventArgs(e.RoutedEvent, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<UserControl x:Class="Ink_Canvas.Controls.ColorPickerButton"
|
||||
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="13" d:DesignWidth="13">
|
||||
<Border x:Name="ButtonBorder"
|
||||
BorderBrush="#CCCCCC"
|
||||
BorderThickness="1"
|
||||
CornerRadius="3"
|
||||
Margin="1,0,1,0"
|
||||
MouseDown="ButtonBorder_MouseDown"
|
||||
MouseLeave="ButtonBorder_MouseLeave"
|
||||
MouseUp="ButtonBorder_MouseUp">
|
||||
<Border.Style>
|
||||
<Style TargetType="Border">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="BorderThickness" Value="2"/>
|
||||
<Setter Property="BorderBrush" Value="#666666"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Border.Style>
|
||||
<Grid>
|
||||
<Path x:Name="CheckPath"
|
||||
Data="M9,20.42L2.79,14.21L5.62,11.38L9,14.77L18.88,4.88L21.71,7.71L9,20.42Z"
|
||||
Stretch="Uniform"
|
||||
Opacity="1.0"
|
||||
Visibility="Collapsed"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Ink_Canvas.Controls
|
||||
{
|
||||
public partial class ColorPickerButton : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
|
||||
nameof(Color), typeof(Color), typeof(ColorPickerButton),
|
||||
new PropertyMetadata(Colors.Black, OnColorChanged));
|
||||
|
||||
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (ColorPickerButton)d;
|
||||
if (button.ButtonBorder != null)
|
||||
button.ButtonBorder.Background = new SolidColorBrush((Color)e.NewValue);
|
||||
}
|
||||
|
||||
public Color Color
|
||||
{
|
||||
get => (Color)GetValue(ColorProperty);
|
||||
set => SetValue(ColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
|
||||
nameof(IsChecked), typeof(bool), typeof(ColorPickerButton),
|
||||
new PropertyMetadata(false, OnIsCheckedChanged));
|
||||
|
||||
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (ColorPickerButton)d;
|
||||
if (button.CheckPath != null)
|
||||
button.CheckPath.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public bool IsChecked
|
||||
{
|
||||
get => (bool)GetValue(IsCheckedProperty);
|
||||
set => SetValue(IsCheckedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CheckIconFillProperty = DependencyProperty.Register(
|
||||
nameof(CheckIconFill), typeof(Brush), typeof(ColorPickerButton),
|
||||
new PropertyMetadata(Brushes.White, OnCheckIconFillChanged));
|
||||
|
||||
private static void OnCheckIconFillChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (ColorPickerButton)d;
|
||||
if (button.CheckPath != null)
|
||||
button.CheckPath.Fill = (Brush)e.NewValue;
|
||||
}
|
||||
|
||||
public Brush CheckIconFill
|
||||
{
|
||||
get => (Brush)GetValue(CheckIconFillProperty);
|
||||
set => SetValue(CheckIconFillProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ButtonSizeProperty = DependencyProperty.Register(
|
||||
nameof(ButtonSize), typeof(double), typeof(ColorPickerButton),
|
||||
new PropertyMetadata(13.0, OnButtonSizeChanged));
|
||||
|
||||
private static void OnButtonSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (ColorPickerButton)d;
|
||||
if (button.ButtonBorder != null)
|
||||
{
|
||||
button.ButtonBorder.Width = (double)e.NewValue;
|
||||
button.ButtonBorder.Height = (double)e.NewValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double ButtonSize
|
||||
{
|
||||
get => (double)GetValue(ButtonSizeProperty);
|
||||
set => SetValue(ButtonSizeProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CheckIconSizeProperty = DependencyProperty.Register(
|
||||
nameof(CheckIconSize), typeof(double), typeof(ColorPickerButton),
|
||||
new PropertyMetadata(8.0, OnCheckIconSizeChanged));
|
||||
|
||||
private static void OnCheckIconSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (ColorPickerButton)d;
|
||||
if (button.CheckPath != null)
|
||||
{
|
||||
button.CheckPath.Width = (double)e.NewValue;
|
||||
button.CheckPath.Height = (double)e.NewValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double CheckIconSize
|
||||
{
|
||||
get => (double)GetValue(CheckIconSizeProperty);
|
||||
set => SetValue(CheckIconSizeProperty, value);
|
||||
}
|
||||
|
||||
public event MouseButtonEventHandler ButtonMouseDown;
|
||||
public event MouseEventHandler ButtonMouseLeave;
|
||||
public event RoutedEventHandler ButtonMouseUp;
|
||||
|
||||
public ColorPickerButton()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += ColorPickerButton_Loaded;
|
||||
}
|
||||
|
||||
private void ColorPickerButton_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ApplyAllProperties();
|
||||
}
|
||||
|
||||
private void ApplyAllProperties()
|
||||
{
|
||||
if (ButtonBorder != null)
|
||||
{
|
||||
ButtonBorder.Background = new SolidColorBrush(Color);
|
||||
ButtonBorder.Width = ButtonSize;
|
||||
ButtonBorder.Height = ButtonSize;
|
||||
}
|
||||
if (CheckPath != null)
|
||||
{
|
||||
CheckPath.Fill = CheckIconFill;
|
||||
CheckPath.Width = CheckIconSize;
|
||||
CheckPath.Height = CheckIconSize;
|
||||
CheckPath.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ButtonMouseDown?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseLeave(object sender, MouseEventArgs e)
|
||||
{
|
||||
ButtonMouseLeave?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void ButtonBorder_MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ButtonMouseUp?.Invoke(this, new RoutedEventArgs(e.RoutedEvent, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user