原文:《模拟MessageBox》 Posted on 2014/01/07

================================================================================

这段时间在用WPF开发项目。界面采用的是类似Win8系统的Metro风格,但是系统自带MessageBox貌似不能被Style,于是重写MessageBox。

之前一直没有写过自定义控件,就不知道怎么下手。于是从网上找了好些资料,发现了这篇博文《WPF 自定义 MessageBox (相对完善版)》。看了代码解释并运行了Demo,觉得根据实际需要改造下应该可以用到项目里来。

以前一直很好奇为什么能Style很多控件,而MessageBox却不能Style,查过.NET Reflector,发现:

public sealed class MessageBox : System.Object

MessageBox类 是直接继承自Object类,实际上是对具体细节进行了封装。

总体结构建立在Vito.K的代码基础之上,我加入的一些功能:

  1. 关闭按钮:
    在MessageBoxModule中定义关闭命令的依赖项属性:

     /// <summary>
    /// 关闭命令
    /// </summary>
    public static readonly DependencyProperty CloseCommandProperty =
    DependencyProperty.Register(
    "CloseCommand",
    typeof(RoutedCommand),
    typeof(MessageBoxModule));
     /// <summary>
    /// 关闭命令
    /// </summary>
    public RoutedCommand CloseCommand
    {
    get { return (RoutedCommand)GetValue(CloseCommandProperty); }
    set { SetValue(CloseCommandProperty, value); }
    }

    设置方法:

     /// <summary>
    /// 设置关闭窗口命令
    /// </summary>
    private void SetupCloseCommand()
    {
    InputGestureCollection inputGestures =
    new InputGestureCollection();
    inputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt)); CloseCommand = new RoutedCommand(
    "CloseCommand",
    typeof(MessageBoxModule),
    inputGestures); CommandBindings.Add(
    new CommandBinding(CloseCommand, CloseCommandExecuted));
    }

    在public MessageBoxModule()方法中调用即可。
    在MessageBoxModule的Style文件中对该命令的使用:

    <Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}" .../>
  2. 通过标题栏拖动MessageBox:
    之前写过的无边框的Windows窗体都是通过Rectangle的MouseLeftButtonDown事件来触发拖动窗体的,然后这个方法在这边行不通,于是又用这方法,还是行不通: 
     <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
    <i:InvokeCommandAction Command="{TemplateBinding DragOverCommand}"/>
    </i:EventTrigger/>
    </i:Interaction.Triggers>

    无意间看到《WPF编程宝典 C# 2010版》第18章中的无外观控件代码中的TemplatePart。照猫画虎地写下了以下代码:
    在Style文件中:

    <Rectangle x:Name="PART_TitleRectangle" .../>

    在MessageBoxModule类中:
    在类前面加上:

     [TemplatePart(Name="PART_TitleRectangle", Type=typeof(Rectangle))]
    internal sealed class MessageBoxModule : Window
    { }

    重载public void OnApplyTemplate()方法:

     public override void OnApplyTemplate()
    {
    base.OnApplyTemplate(); // 为TitleRectangle添加MouseLeftButtonDown事件处理程序
    Rectangle rect = GetTemplateChild("PART_TitleRectangle") as Rectangle;
    if (rect != null)
    {
    rect.MouseLeftButtonDown += (sender, e) => { DragMove(); };
    }
    }
  3. 显示详细信息:
    主要是Style中几个相关控件的Visibility的设置,依据是MessageBoxModule类中的指定值,即DependencyProperty MessageDetailProperty是否为空来选择是否启用显示相信信息功能:
     <DockPanel x:Name="RootLayoutPanel">
    ...
    <Separator Visibility="{Binding Path=Visibility, ElementName=MessageDetailTextBlock}" DockPanel.Dock="Bottom"/>
    <!--信息区-->
    <DockPanel x:Name="ContentDockPanel" DockPanel.Dock="Top">
    ...
    <TextBlock x:Name="MessageTextBlock" Text="{TemplateBinding Message}" DockPanel.Dock="Top" />
    <Grid Visibility="{Binding Path=IsChecked, ElementName=DetailInfoToggleButton, Converter={converter:BooleanToVisibilityConverter}}" >
    <TextBlock x:Name="MessageDetailTextBlock"
    Text="{TemplateBinding MessageDetail}"
    Visibility="{TemplateBinding MessageDetail, Converter={converter:StringToVisibilityConverter}}"
    DockPanel.Dock="Bottom" />
    </Grid>
    </DockPanel>
    </DockPanel>
  4. 加入MessageBoxImage:
    通过MessageBoxModule类中的DependencyProperty MessageBoxIconTypeProperty来指定显示哪种类型的图片: 
     /// <summary>
    /// 消息框图标
    /// </summary>
    public static readonly DependencyProperty MessageBoxIconTypeProperty =
    DependencyProperty.Register(
    "MessageBoxIconType",
    typeof(MessageBoxIcon),
    typeof(MessageBoxModule),
    new PropertyMetadata(MessageBoxIcon.None));

    MessageBoxIcon是枚举类型类似于MessageBoxImage的一个枚举类型:

     /// <summary>
    /// 图标
    /// </summary>
    public enum MessageBoxIcon
    {
    None = ,
    Error = 0x10,
    //Hand = 0x10,
    //Stop = 0x10,
    Question = 0x20,
    Warning = 0x30,
    //Exclamation = 0x30,
    //Asterisk = 0x40,
    //Information = 0x40,
    }

    在Style文件中:

     <!--信息区-->
    <DockPanel x:Name="ContentDockPanel" DockPanel.Dock="Top" Margin="15,10">
    <Image x:Name="MB_MistakeImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_mistake.png"/>
    <Image x:Name="MB_QueryImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_query.png"/>
    <Image x:Name="MB_WarningImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_warning.png"/>
    ...
    </DockPanel>

    用ControlTemplate.Triggers进行控制对应Image的Visibility属性:

     <ControlTemplate.Triggers>
    <Trigger Property="MessageBoxIconType" Value="Error">
    <Setter Property="Visibility" TargetName="MB_MistakeImage" Value="Visible"/>
    </Trigger>
    <Trigger Property="MessageBoxIconType" Value="Question">
    <Setter Property="Visibility" TargetName="MB_QueryImage" Value="Visible"/>
    </Trigger>
    <Trigger Property="MessageBoxIconType" Value="Warning">
    <Setter Property="Visibility" TargetName="MB_WarningImage" Value="Visible"/>
    </Trigger>
    </ControlTemplate.Triggers>

【END】

模拟MessageBox的更多相关文章

  1. WPF 自定义 MessageBox (相对完善版)

    WPF 自定义 MessageBox (相对完善版)     基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...

  2. WPF 自定义 MessageBox (相对完善版 v1.0.0.6)

    基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...

  3. ASP.NET中MessageBox的实现

    asp.net中没有MessageBox这个控件,固然可以插入Winform里的MessageBox,但一般不提倡,所以只能变通实现,主要有这几种方法: 1.直接利用javascript的alert和 ...

  4. C语言嵌入式系统编程修炼之四:屏幕操作

    汉字处理 现在要解决的问题是,嵌入式系统中经常要使用的并非是完整的汉字库,往往只是需要提供数量有限的汉字供必要的显示功能.例如,一个微波炉的LCD上没有必要提供显示"电子邮件"的功 ...

  5. C语言嵌入式系统编程修炼

    C语言嵌入式系统编程修炼 2008-08-19 作者:宋宝华 来源:天极网 C语言嵌入式系统编程修炼之背景篇 本文的讨论主要围绕以通用处理器为中心的协议处理模块进行,因为它更多地牵涉到具体的C语言编程 ...

  6. [读书笔记2]《C语言嵌入式系统编程修炼》

    第3章 屏幕操作   3.1 汉字处理 现在要解决的问题是,嵌入式系统中经常要使用的并非是完整的汉字库,往往只是需要提供数量有限的汉字供必要的显示功能.例如,一个微波炉的LCD上没有必要提供显示&qu ...

  7. c# winform 自动关闭messagebox 模拟回车

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 一个简单的Webservice的demo,简单模拟服务

    前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...

  9. NetworkComms V3 模拟登陆

    演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...

随机推荐

  1. Android 学习之异常总结--java.lang.IllegalStateException:Could not execute method of the activity

    在android学习过程中通常会遇到java.lang.IllegalStateException:Could not execute method of the activity这个错误:非法状态的 ...

  2. Java null String

    In Java, the String will have different usage. Example: public class Test { public static void main( ...

  3. 从零开始学Bootstrap(2)

    继从零开始学Bootstrap(1)后,我们需要实际做一些页面,边学边做.因为前端是一项非常注意实践的技术,知识点太多.太琐碎了,所以我们只能边学边做.根据我们想要做的效果,去查相应的资料.不要想着把 ...

  4. 如何向Openstack社区提交一个新项目

    前几天有个朋友问我:自己有一个idea不错的项目,也把基本的框架写好了,想贡献到Openstack社区,却不知道应该怎么做.正好之前我有过类似的经历,那么来分享一下我是如何向Openstack社区提交 ...

  5. StringBuffer&StringBuilder区别详解

    序言 StringBuffer与StringBuilder是java.lang包下被大家熟知的两个类.其异同为:一.长度都是可扩充的:二.StringBuffer是线程安全的,StringBuilde ...

  6. iOS 同一设备内的应用之间资源共享的实现

    我们都知道,iOS为安全考虑,各应用只能使用其自已的应用沙盒内的存储空间,各应用之间是不能互相访问彼此的沙盒空间的. 另外,iOS设备都没有外置存储卡,这样,象 Android 设备间共同访问外置存储 ...

  7. Win7版IE10浏览器正式版官方下载地址

    • 简体中文,Win7 SP1 32位版IE10下载: http://download.microsoft.com/download/4/1/4/4149BFB1-AC27-401D-943F-DA1 ...

  8. 新安装的VS的一些设置

    古语云:工欲善其事必先利其器 为了方便我们开发,应该设置好VS的一些配置,安装一些辅助插件 1 设置字体和背景等 设置字体为 console 10大小 背景设为护眼颜色 85 90 205 这三个值 ...

  9. IOS开发 应用程序图标数字角标

    其实实现这个功能很简单,只要调用UIApplication即可.   用法用例:[UIApplication sharedApplication].applicationIconBadgeNumber ...

  10. Asp.net Core WebApi 使用Swagger做帮助文档,并且自定义Swagger的UI

    WebApi写好之后,在线帮助文档以及能够在线调试的工具是专业化的表现,而Swagger毫无疑问是做Docs的最佳工具,自动生成每个Controller的接口说明,自动将参数解析成json,并且能够在 ...