WPF 自定义消息框

Posted 向着梦想前进的男人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 自定义消息框相关的知识,希望对你有一定的参考价值。

说明:做一个简单的消息框,能够根据自定义显示图片和按钮。

步骤:

1.其实消息框就是一个Window,首先设置窗体的显示位置,显示大小,要让它看着有消息框的样子。这个很容易,代码如下:

Height="206" Width="420" WindowStartupLocation="CenterScreen" BorderThickness="1,0,1,1" ShowIcon="False" Closed="DXRibbonWindow_Closed" ResizeMode="NoResize"

2.然后我们要画出消息框,就是一个Grid,分配好空间显示图片和弹框提示,底部显示按钮,这里要注意按钮的显示是自定义选择的,所以位置设置为相对位置。代码如下:

 <Grid x:Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <!--显示图片和文本-->
        <StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
            <Image Source="{Binding ImagePath}" Width="62" Height="62" Margin="40,20,20,20"/>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Width="280" TextAlignment="Left"
                       Text="{Binding MessageBoxText}" FontSize="12"/>
        </StackPanel>
        <!--Button Margin(坐上右下)-->
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="确 定" x:Name="OkButton" Width="80" Height="25" Click="OkButton_Click" Margin="10,0,15,0" IsDefault="True"
                    Visibility="{Binding OkButtonVisibility,Mode=OneWay}"/>
            <Button Content="" x:Name="YesButton" Width="80" Height="25" Click="YesButton_Click" Margin="10,0,15,0" 
                    Visibility="{Binding YesButtonVisibility,Mode=OneWay}"/>
            <Button Content="" x:Name="NoButton" Width="80" Height="25" Click="NoButton_Click" Margin="10,0,15,0"
                    Visibility="{Binding NoButtonVisibility,Mode=OneWay}"/>
            <Button Content="取消" x:Name="CancelButton" Width="80" Height="25" Click="CancelButton_Click" Margin="10,0,15,0"
                    Visibility="{Binding CancelButtonVisibility}"/>
        </StackPanel>
    </Grid>

3.定义枚举值,分别为按钮的返回值,消息框的返回值以及图标类型。代码如下:

  /// <summary>
    /// 显示按钮类型
    /// </summary>
    public enum CustomMessageBoxButton
    {
        OK=0,
        OKCancel=1,
        YesNo=2,
        YesNoCancel=3
    }
    /// <summary>
    /// 消息框的返回值
    /// </summary>
    public enum CustomMessageBoxResult
    {
        //用户直接关闭了消息窗口
        None = 0,
        //用户点击确定按钮
        OK = 1,
        //用户点击取消按钮
        Cancel = 2,
        //用户点击是按钮
        Yes = 3,
        //用户点击否按钮
        No = 4
    }
    /// <summary>
    /// 图标类型
    /// </summary>
    public enum CustomMessageBoxIcon
    {
        None = 0,
        Error = 1,
        Question = 2,
        Warning = 3
    }

4.定义属性,并且在后置代码的构造函数中将按钮全部显示为不可见。代码如下:

    #region Filed
        /// <summary>
        /// 显示的内容
        /// </summary>
        public string MessageBoxText { get; set; }
        /// <summary>
        /// 显示的图片
        /// </summary>
        public string ImagePath { get; set; }
        /// <summary>
        /// 控制显示 OK 按钮
        /// </summary>
        public Visibility OkButtonVisibility { get; set; }
        /// <summary>
        /// 控制显示 Cacncel 按钮
        /// </summary>
        public Visibility CancelButtonVisibility { get; set; }
        /// <summary>
        /// 控制显示 Yes 按钮
        /// </summary>
        public Visibility YesButtonVisibility { get; set; }
        /// <summary>
        /// 控制显示 No 按钮
        /// </summary>
        public Visibility NoButtonVisibility { get; set; }
        /// <summary>
        /// 消息框的返回值
        /// </summary>
        public CustomMessageBoxResult Result { get; set; }

        #endregion
  public CustomMessageBoxWindow()
        {            
            InitializeComponent();
            this.DataContext = this;

            OkButtonVisibility = Visibility.Collapsed;
            CancelButtonVisibility = Visibility.Collapsed;
            YesButtonVisibility = Visibility.Collapsed;
            NoButtonVisibility = Visibility.Collapsed;

            Result = CustomMessageBoxResult.None;
        }

5.在按钮的事件中为按下按钮返回值赋值并且关闭窗口:

  private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.OK;
            this.Close();
        }

        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.Yes;
            this.Close();
        }

        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.No;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.Cancel;
            this.Close();
        }

6.定义Show方法,根据用户自定义弹出消息框显示:

   public static CustomMessageBoxResult Show(string messageBoxText, CustomMessageBoxButton messageBoxButton, CustomMessageBoxIcon messageBoxImage)
        {
            CustomMessageBoxWindow window = new CustomMessageBoxWindow();
            window.Owner = Application.Current.MainWindow;
            window.Topmost = true;
            window.MessageBoxText = messageBoxText;
            switch (messageBoxImage)
            {
                case CustomMessageBoxIcon.Question:
                    window.ImagePath = @"/Images/question.png";
                    break;
                case CustomMessageBoxIcon.Error:
                case CustomMessageBoxIcon.Warning:
                    window.ImagePath = @"/Images/alert.png";
                    break;
            }
            switch (messageBoxButton)
            {
                case CustomMessageBoxButton.OK:
                    window.OkButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.OKCancel:
                    window.OkButtonVisibility = Visibility.Visible;
                    window.CancelButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.YesNo:
                    window.YesButtonVisibility = Visibility.Visible;
                    window.NoButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.YesNoCancel:
                    window.YesButtonVisibility = Visibility.Visible;
                    window.NoButtonVisibility = Visibility.Visible;
                    window.CancelButtonVisibility = Visibility.Visible;
                    break;
                default:
                    window.OkButtonVisibility = Visibility.Visible;
                    break;
            }

            window.ShowDialog();
            return window.Result;
        }

7.效果如下:

8.消息框本身就很简单,但是网上的好多例子说的有些复杂了,所以自己做了一个。消息框的显示方法我只写了一个,如果有更多需求可以重载方法就好。另外,我的VS上装了DevExpress,我用的是Dev的Ribbon Window,没装DEV的可以用普通Window,装了Dev的还是要删除引用添加自己的引用,最后源码中附上了改变窗体显示主题的代码。源码在最后上传给大家参考。

 源码下载:http://files.cnblogs.com/files/damon-xu/Demo1.rar

以上是关于WPF 自定义消息框的主要内容,如果未能解决你的问题,请参考以下文章

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

请问一下wpf能否实现消息提示框 就像qq消息的那种 右下角弹出 ?能的话 请贴出代码

wpf怎么自定义窗口

wpf DataGrid自定义相关问题

WPF自定义数字输入框控件

WPF 自定义控件并使用(例如带水印和字体图标的文本框)