您的位置:首页 > Web前端 > CSS

WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

2015-12-02 09:24 811 查看
一.前言

  申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接。

  本文主要内容:

自定义Window窗体样式;

基于自定义窗体实现自定义MessageBox消息提示框;

二.自定义Window窗体样式

  自定义的Window窗体效果:

/// <summary>
/// MessageBoxXxaml.xaml 的交互逻辑
/// </summary>
public partial class MessageBoxX : WindowBase
{
/// <summary>
/// 结果,用户点击确定Result=true;
/// </summary>
public bool Result { get; private set; }

private static readonly Dictionary<string, Brush> _Brushes = new Dictionary<string, Brush>();

public MessageBoxX(EnumNotifyType type, string mes)
{
InitializeComponent();
this.txtMessage.Text = mes;
//type
btnCancel.Visibility = Visibility.Collapsed;
this.SetForeground(type);
switch (type)
{
case EnumNotifyType.Error:
this.ficon.Text = "\ue644";
break;
case EnumNotifyType.Warning:
this.ficon.Text = "\ue60b";
break;
case EnumNotifyType.Info:
this.ficon.Text = "\ue659";
break;
case EnumNotifyType.Question:
this.ficon.Text = "\ue60e";
this.btnCancel.Visibility = Visibility.Visible;
break;
}
}

private void SetForeground(EnumNotifyType type)
{
string key = type.ToSafeString() + "Foreground";
if (!_Brushes.ContainsKey(key))
{
var b = this.TryFindResource(key) as Brush;
_Brushes.Add(key, b);
}
this.Foreground = _Brushes[key];
}

private void btnOK_Click(object sender, RoutedEventArgs e)
{
this.Result = true;
this.Close();
e.Handled = true;
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Result = false;
this.Close();
e.Handled = true;
}

/********************* public static method **************************/

/// <summary>
/// 提示错误消息
/// </summary>
public static void Error(string mes, Window owner = null)
{
Show(EnumNotifyType.Error, mes, owner);
}

/// <summary>
/// 提示普通消息
/// </summary>
public static void Info(string mes, Window owner = null)
{
Show(EnumNotifyType.Info, mes, owner);
}

/// <summary>
/// 提示警告消息
/// </summary>
public static void Warning(string mes, Window owner = null)
{
Show(EnumNotifyType.Warning, mes, owner);
}

/// <summary>
/// 提示询问消息
/// </summary>
public static bool Question(string mes, Window owner = null)
{
return Show(EnumNotifyType.Question, mes, owner);
}

/// <summary>
/// 显示提示消息框,
/// owner指定所属父窗体,默认参数值为null,则指定主窗体为父窗体。
/// </summary>
private static bool Show(EnumNotifyType type, string mes, Window owner = null)
{
var res = true;
Application.Current.Dispatcher.Invoke(new Action(() =>
{
MessageBoxX nb = new MessageBoxX(type, mes) { Title = type.GetDescription() };
nb.Owner = owner ?? Application.Current.MainWindow;
nb.ShowDialog();
res = nb.Result;
}));
return res;
}

/// <summary>
/// 通知消息类型
/// </summary>
public enum EnumNotifyType
{
[Description("错误")]
Error,
[Description("警告")]
Warning,
[Description("提示信息")]
Info,
[Description("询问信息")]
Question,
}
}


View Code

附录:参考引用

WPF自定义控件与样式(1)-矢量字体图标(iconfont)

WPF自定义控件与样式(2)-自定义按钮FButton

WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

WPF自定义控件与样式(6)-ScrollViewer与ListBox自定义样式

WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu

WPF自定义控件与样式(10)-进度控件ProcessBar自定义样

WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现

WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

版权所有,文章来源:http://www.cnblogs.com/anding

个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: