using BPA.UIControl.Commons.KnownBoxes;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace BPA.UIControl
{
///
/// 消息框卡片
///
[TemplatePart(Name = TransitionPartName, Type = typeof(Transition))]
[TemplatePart(Name = OkButtonPartName, Type = typeof(Button))]
[TemplatePart(Name = CancelButtonPartName, Type = typeof(Button))]
[TemplatePart(Name = YesButtonPartName, Type = typeof(Button))]
[TemplatePart(Name = NoButtonPartName, Type = typeof(Button))]
public class MessageBoxCard : Control
{
///
/// 转换动画名称
///
public const string TransitionPartName = "Path_Transition";
///
/// 确认按钮名称
///
public const string OkButtonPartName = "PART_OkButton";
///
/// 取消按钮名称
///
public const string CancelButtonPartName = "PART_CancelButton";
///
/// 是按钮名称
///
public const string YesButtonPartName = "PART_YesButton";
///
/// 否按钮名称
///
public const string NoButtonPartName = "PART_NoButton";
///
/// 消息框结果事件处理
///
///
///
public delegate void MessageBoxResultRoutedEventHandler(object sender, MessageBoxResultRoutedEventArgs e);
static MessageBoxCard()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxCard), new FrameworkPropertyMetadata(typeof(MessageBoxCard)));
}
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (GetTemplateChild(OkButtonPartName) is ButtonBase okButton)
{
okButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.OK);
}
if (GetTemplateChild(CancelButtonPartName) is ButtonBase cancelButton)
{
cancelButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.Cancel);
}
if (GetTemplateChild(YesButtonPartName) is ButtonBase yesButton)
{
yesButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.Yes);
}
if (GetTemplateChild(NoButtonPartName) is ButtonBase noButton)
{
noButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.No);
}
if (GetTemplateChild(TransitionPartName) is Transition transition)
{
transition.Closed += (sender, e) =>
{
RoutedEventArgs eventArgs = new RoutedEventArgs(ClosedEvent, this);
this.RaiseEvent(eventArgs);
};
}
}
#region 事件
///
/// 返回结果事件
///
public static readonly RoutedEvent ReturnResultEvent = EventManager.RegisterRoutedEvent("ReturnResult", RoutingStrategy.Bubble, typeof(MessageBoxResultRoutedEventHandler), typeof(MessageBoxCard));
///
/// ///
/// 返回结果事件处理
///
///
public event MessageBoxResultRoutedEventHandler ReturnResult
{
add { AddHandler(ReturnResultEvent, value); }
remove { RemoveHandler(ReturnResultEvent, value); }
}
///
/// 关闭消息事件
///
public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Close", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MessageBoxCard));
///
/// 关闭消息事件处理
///
public event RoutedEventHandler Closed
{
add { AddHandler(ClosedEvent, value); }
remove { RemoveHandler(ClosedEvent, value); }
}
#endregion 事件
#region 依赖属性
///
/// 是否显示
///
public static readonly DependencyProperty IsShowProperty =
DependencyProperty.Register("IsShow", typeof(bool), typeof(MessageBoxCard), new PropertyMetadata(BooleanBoxes.FalseBox));
///
/// 是否显示
///
public bool IsShow
{
get { return (bool)GetValue(IsShowProperty); }
set { SetValue(IsShowProperty, BooleanBoxes.Box(value)); }
}
///
/// 消息内容
///
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(string), typeof(MessageBoxCard), new PropertyMetadata(default(string)));
///
/// 消息内容
///
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
///
/// 标题
///
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title", typeof(string), typeof(MessageBoxCard), new PropertyMetadata(default(string)));
///
/// 标题
///
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
///
/// 圆角半径
///
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
"CornerRadius", typeof(CornerRadius), typeof(MessageBoxCard), new PropertyMetadata(default(CornerRadius)));
///
/// 圆角半径
///
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
///
/// 主题颜色
///
public static readonly DependencyProperty ThemeColorBrushProperty = DependencyProperty.Register(
"ThemeColorBrush", typeof(Brush), typeof(MessageBoxCard), new PropertyMetadata(default(Brush)));
///
/// 主题颜色
///
public Brush ThemeColorBrush
{
get { return (Brush)GetValue(ThemeColorBrushProperty); }
set { SetValue(ThemeColorBrushProperty, value); }
}
///
/// 图标类型
///
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
"Type", typeof(MessageBoxType), typeof(MessageBoxCard), new PropertyMetadata(default(MessageBoxType)));
///
/// 图标类型
///
public MessageBoxType Type
{
get { return (MessageBoxType)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
///
/// 是否显示阴影
///
public static readonly DependencyProperty ShowShadowProperty = DependencyProperty.Register(
"ShowShadow", typeof(bool), typeof(MessageBoxCard), new PropertyMetadata(BooleanBoxes.FalseBox));
///
/// 是否显示阴影
///
public bool ShowShadow
{
get { return (bool)GetValue(ShowShadowProperty); }
set { SetValue(ShowShadowProperty, BooleanBoxes.Box(value)); }
}
///
/// 消息框按钮类型
///
public static readonly DependencyProperty MessageBoxButtonProperty = DependencyProperty.Register(
"MessageBoxButton", typeof(MessageBoxButton), typeof(MessageBoxCard), new PropertyMetadata(default(MessageBoxButton)));
///
/// 消息框按钮类型
///
public MessageBoxButton MessageBoxButton
{
get { return (MessageBoxButton)GetValue(MessageBoxButtonProperty); }
set { SetValue(MessageBoxButtonProperty, value); }
}
#endregion 依赖属性
private void InternalReturnResult(MessageBoxCard card, MessageBoxResult result)
{
IsShow = false;
MessageBoxResultRoutedEventArgs eventArgs = new MessageBoxResultRoutedEventArgs(ReturnResultEvent, result, card);
card.RaiseEvent(eventArgs);
}
}
///
/// 消息框框结果路由参数
///
public class MessageBoxResultRoutedEventArgs : RoutedEventArgs
{
///
/// 结果
///
public MessageBoxResult Result { get; set; }
///
/// 消息框
///
public MessageBoxCard Card { get; set; }
///
/// Initializes a new instance of the class.
///
/// The routed event.
/// The result.
/// 消息框卡片
public MessageBoxResultRoutedEventArgs(RoutedEvent routedEvent, MessageBoxResult result, MessageBoxCard card) : base(routedEvent)
{
Result = result;
Card = card;
}
}
}