namespace AntdWpf.Controls
{
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ButtonBase = System.Windows.Controls.Button;
///
/// To trigger an operation.
///
[TemplatePart(Name = PART_Border, Type = typeof(FrameworkElement))]
[TemplateVisualState(Name = "Loaded", GroupName = "LoadStates")]
[TemplateVisualState(Name = "Unloaded", GroupName = "LoadStates")]
public class Button : ButtonBase
{
#region Fields
private const string PART_Border = "PART_Border";
private FrameworkElement border;
private VisualState mouseOverState;
private VisualState pressedState;
private VisualState focusedState;
#endregion
#region Properties
public static readonly DependencyProperty GhostProperty =
DependencyProperty.Register("Ghost", typeof(bool), typeof(Button), new PropertyMetadata(false, OnEffectBrushChanged));
///
/// Gets/sets whether to make the background transparent and invert text and border colors.
///
public bool Ghost
{
get { return (bool)GetValue(GhostProperty); }
set { SetValue(GhostProperty, value); }
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(string), typeof(Button), new PropertyMetadata(null));
///
/// Gets/sets the icon type of the button.
///
public string Icon
{
get { return (string)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty LoadingProperty =
DependencyProperty.Register("Loading", typeof(bool), typeof(Button), new PropertyMetadata(false, OnLoadingChanged));
///
/// Gets/sets the loading state of the button.
///
public bool Loading
{
get { return (bool)GetValue(LoadingProperty); }
set { SetValue(LoadingProperty, value); }
}
private static void OnLoadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as Button).SetLoadVisualState();
}
private void SetLoadVisualState()
{
VisualStateManager.GoToState(this, (Loading ? "Loaded" : "Unloaded"), true);
}
public static readonly DependencyProperty ShapeProperty =
DependencyProperty.Register("Shape", typeof(Shapes), typeof(Button), new PropertyMetadata(Shapes.Square));
///
/// Gets/sets the shape of button.
///
public Shapes Shape
{
get { return (Shapes)GetValue(ShapeProperty); }
set { SetValue(ShapeProperty, value); }
}
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register("Size", typeof(Sizes?), typeof(Button), new PropertyMetadata(null));
///
/// Gets/sets the size of the button.
///
public Sizes? Size
{
get { return (Sizes?)GetValue(SizeProperty); }
set { SetValue(SizeProperty, value); }
}
public static readonly DependencyProperty TypeProperty =
DependencyProperty.Register("Type", typeof(ButtonType?), typeof(Button), new PropertyMetadata(null));
///
/// Gets/sets the type of the button.
///
public ButtonType? Type
{
get { return (ButtonType?)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
public static readonly DependencyProperty EffectBrushProperty = DependencyProperty.Register(
"EffectBrush",
typeof(Brush),
typeof(Button),
new FrameworkPropertyMetadata(
Brushes.Transparent,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits,
OnEffectBrushChanged));
///
/// Gets/sets the border effect brush of the button.
///
public Brush EffectBrush
{
get { return (Brush)GetValue(EffectBrushProperty); }
set { SetValue(EffectBrushProperty, value); }
}
private static void OnEffectBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as Button).SetVisualStateAnimation();
}
///
/// Force background transparent in Ghost state.
///
private static object OnBackgroundCoerceValue(DependencyObject d, object baseValue)
{
var button = d as Button;
if (button.Ghost)
{
return Brushes.Transparent;
}
return baseValue;
}
#endregion
#region Constructors
static Button()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Button), new FrameworkPropertyMetadata(typeof(Button)));
BackgroundProperty.OverrideMetadata(typeof(Button), new FrameworkPropertyMetadata { CoerceValueCallback = OnBackgroundCoerceValue });
}
#endregion
#region Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
border = GetTemplateChild(PART_Border) as FrameworkElement;
mouseOverState = GetTemplateChild("MouseOver") as VisualState;
focusedState = GetTemplateChild("Focused") as VisualState;
pressedState = GetTemplateChild("Pressed") as VisualState;
SetVisualStateAnimation();
SetLoadVisualState();
}
#endregion
#region Private Methods
private void SetVisualStateAnimation()
{
// No initialization or no need for me to handle.
if (border == null || mouseOverState == null && focusedState == null && pressedState == null) return;
// Unable to extract color.
if (EffectBrush is SolidColorBrush brush)
{
var isShape = border is Shape;
Func