WPF custom message box, wpf custom message

Source: Internet
Author: User

WPF custom message box, wpf custom message

Note:Create a simple message box that allows you to display images and buttons according to your needs.

Steps:

1. In fact, the message box is a Window. First, set the display position and display size of the form to make it look like there is a message box. The Code is as follows:

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

2. then we need to draw a message box, which is a Grid. The allocated space shows the image and the prompt in the pop-up box. The button is displayed at the bottom. Note that the display of the button is customized, therefore, the location is set to relative. The Code is as follows:

<Grid x: Name = "grid"> <Grid. RowDefinitions> <RowDefinition/> <RowDefinition Height = "50"/> </Grid. RowDefinitions> <! -- Display images and text --> <StackPanel Grid. row = "0" VerticalAlignment = "Center" Orientation = "Horizontal"> <Image Source = "{Binding ImagePath}" Width = "62" Height = "62" Margin = "40, 20, 280 "/> <TextBlock VerticalAlignment =" Center "placement =" Left "TextWrapping =" WrapWithOverflow "Width =" "TextAlignment =" Left "Text =" {Binding MessageBoxText} "FontSize = "12"/> </StackPanel> <! -- Button Margin (lower right) --> <StackPanel Grid. row = "1" Orientation = "Horizontal" HorizontalAlignment = "Right"> <Button Content = "OK" x: name = "OkButton" Width = "80" Height = "25" Click = "OkButton_Click" Margin = "10, 0, 15, 0" IsDefault = "True" Visibility = "{Binding OkButtonVisibility, mode = OneWay} "/> <Button Content =" yes "x: name = "YesButton" Width = "80" Height = "25" Click = "YesButton_Click" Margin = "10, 0, 15, 0" Visibility = "{Binding YesButtonVisibility, mode = OneWay} "/> <Button Content =" no "x: name = "NoButton" Width = "80" Height = "25" Click = "NoButton_Click" Margin = "10, 0, 15, 0" Visibility = "{Binding NoButtonVisibility, mode = OneWay} "/> <Button Content =" cancel "x: name = "CancelButton" Width = "80" Height = "25" Click = "CancelButton_Click" Margin = "10, 0, 15, 0 "Visibility =" {Binding CancelButtonVisibility} "/> </StackPanel> </Grid>

3. Define enumerated values, which are the return values of the button, the return values of the message box, And the icon type. The Code is as follows:

/// <Summary> /// display button type /// </summary> public enum CustomMessageBoxButton {OK = 0, OKCancel = 1, YesNo = 2, yesNoCancel = 3} /// <summary> // return value of the message box /// </summary> public enum CustomMessageBoxResult {// The user closes the message window None = 0, // The user clicks OK = 1, // the user clicks the Cancel button Cancel = 2, // the user clicks Yes = 3, // click the No button No = 4} /// <summary> /// icon type // </summary> public enum CustomMessageBoxIcon {None = 0, Error = 1, question = 2, Warning = 3}

4. Define attributes and make all buttons invisible in the Post-code constructor. The Code is as follows:

# Region Filed /// <summary> /// displayed content /// </summary> public string MessageBoxText {get; set ;} /// <summary> /// displayed image /// </summary> public string ImagePath {get; set ;} /// <summary> // control the OK button. /// </summary> public Visibility OkButtonVisibility {get; set ;} /// <summary> // control the Cacncel button. /// </summary> public Visibility CancelButtonVisibility {get; set ;} /// <summary> // control the Yes button. /// </summary> public Visibility YesButtonVisibility {get; set ;} /// <summary> /// control the No button. /// </summary> public Visibility NoButtonVisibility {get; set ;} /// <summary> /// return value of the message box /// </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. assign values to the values returned by pressing the button in the button event and close the window:

  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. Define the Show method. The pop-up message box is displayed as follows:

   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. The effect is as follows:

8. The message box itself is very simple, but many examples on the Internet are complicated, so I made one myself. I only wrote one display method for the message box. If you have more requirements, you can reload the method. In addition, my VS has DevExpress installed. I use Dev's Ribbon Window. If DEV is not installed, I can use a normal Window. If Dev is installed, I still need to delete the reference and add my own reference, finally, the Code for changing the display topic of the form is included in the source code. The source code is uploaded at the end for your reference.

Source code download: http://files.cnblogs.com/files/damon-xu/Demo1.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.