wpf 带有“是”和“否”按钮的消息框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22522712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:16:31  来源:igfitidea点击:

Message Box with Yes and No buttons

c#wpfsilverlightwindows-phone-8windows-phone

提问by Goofy

I am working on Windows Phone 8, i have scenario where in the message box i am questioning the user, So the user has to click YES or NO.

我在 Windows Phone 8 上工作,我在消息框中询问用户,因此用户必须单击“是”或“否”。

But the meassage box is having only OK and Cancel buttons, which i dont need it.

但是消息框只有确定和取消按钮,我不需要它。

Here is my code:

这是我的代码:

MessageBoxResult m = MessageBox.Show("Are you sure? This will delete all your data", "Attention!", MessageBoxButton.OKCancel);
            if (m == MessageBoxResult.Cancel)
            {

            }
            else
            {
                MessageBox.Show("Data Deleted", "Done", MessageBoxButton.OK);
            } 

How can i modify this to YES and NO buttons ? and also can i have 3 buttons as well with custom text on buttons?

如何将其修改为“是”和“否”按钮?我也可以有 3 个按钮以及按钮上的自定义文本吗?

回答by NullReferenceException

You can use this custom Message Box

您可以使用此自定义消息框

SLMessageBox.xaml

SLMessageBox.xaml

<controls:ChildWindow x:Class="SLMessageBoxEX.SLMessageBox"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
                  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
                  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                  Title="SL Custom Message Box"
                  Width="320"
                  Height="150">
<controls:ChildWindow.Resources>
    <Style x:Key="TextBoxStyle" TargetType="TextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid x:Name="RootElement">
                        <ScrollViewer x:Name="ContentElement"
                                      Background="{TemplateBinding Background}"
                                      BorderThickness="0"
                                      Padding="{TemplateBinding Padding}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</controls:ChildWindow.Resources>
<controls:ChildWindow.Style>
    <StaticResource ResourceKey="AlertBoxStyle" />
</controls:ChildWindow.Style>

<Grid x:Name="LayoutRoot" Margin="2">
     <Grid.RowDefinitions>
        <RowDefinition Height="80" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBox x:Name="txtMsg"
             Width="195"
             Margin="59,22,0,22"
             HorizontalAlignment="Left"
             VerticalAlignment="Center"
             Background="#02FFFFFF"
             BorderBrush="{x:Null}"
             Cursor="Arrow"
             FontSize="11"
             FontWeight="SemiBold"
             IsReadOnly="True"
             SelectionBackground="#FF727272"
             Style="{StaticResource TextBoxStyle}"
             Text="SysInformation Healthcare India Pvt Ltd."
             TextAlignment="Center"
             TextWrapping="Wrap" />
    <StackPanel Grid.Row="1"
                HorizontalAlignment="Center"
                Orientation="Horizontal">
        <Button x:Name="btnYes"
                Width="65"
                Height="23"
                Margin="10,0,5,0"
                Click="OKButton_Click"
                Content="Yes"
                Style="{StaticResource ButtonStyle1}" />
        <Button x:Name="btnNo"
                Width="65"
                Height="23"
                Margin="5,0,5,0"
                Click="btnNo_Click"
                Content="No"
                Style="{StaticResource ButtonStyle1}" />
        <Button x:Name="btnCancel"
                Width="65"
                Height="23"
                Margin="5,0,5,0"
                Click="CancelButton_Click"
                Content="Cancel"
                Style="{StaticResource ButtonStyle1}" />
    </StackPanel>

    <Image x:Name="imgIcon"
           Width="45"
           Height="40"
           Margin="10,20,0,20"
           HorizontalAlignment="Left"
           VerticalAlignment="Center">
        <Image.Effect>
            <DropShadowEffect Color="#FF434343" />
        </Image.Effect>
    </Image>

</Grid>

SLMessageBox.xaml.cs

SLMessageBox.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SLMessageBoxEX
{
public partial class SLMessageBox : ChildWindow
{
    public delegate void MessageBoxClosedDelegate(MessageBoxResult result);
    public event MessageBoxClosedDelegate OnMessageBoxClosed;
    public MessageBoxResult Result { get; set; }

    public SLMessageBox(string title, string message, MessageBoxButtons buttons,MessageBoxIcon icon)
    {
        InitializeComponent();
        this.Closed += new EventHandler(MessageBoxChildWindow_Closed);

        this.Title = title;
        this.txtMsg.Text = message;
        DisplayButtons(buttons);
        DisplayIcon(icon);
    }

    public enum MessageBoxButtons
    {
        Ok, YesNo, YesNoCancel, OkCancel
    }

    public enum MessageBoxIcon
    {
        Question, Information, Error, None, Warning, Logout, ThankYou
    }

    private void DisplayIcon(MessageBoxIcon icon)
    {
        switch (icon)
        {
            case MessageBoxIcon.Error:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBox;component/Images/error.png", UriKind.Relative));
                break;

            case MessageBoxIcon.Information:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/Information.png", UriKind.Relative));
                break;

            case MessageBoxIcon.Question:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/SIHIImages/question.png", UriKind.Relative));
                break;

            case MessageBoxIcon.Warning:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/warning.png", UriKind.Relative));
                break;

            case MessageBoxIcon.None:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBox;component/Images/Information.png", UriKind.Relative));
                break;

            case MessageBoxIcon.Logout:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/logout1.png", UriKind.Relative));
                break;

            case MessageBoxIcon.ThankYou:
                imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/ThankYou.png", UriKind.Relative));
                break;
        }
    }

    private void DisplayButtons(MessageBoxButtons buttons)
    {

        switch (buttons)
        {
            case MessageBoxButtons.Ok:
                btnCancel.Visibility = Visibility.Collapsed;
                btnNo.Visibility = Visibility.Collapsed;
                btnYes.Visibility = Visibility.Visible;
                btnYes.Content = "Ok";
                break;

            case MessageBoxButtons.OkCancel:
                btnCancel.Visibility = Visibility.Visible;
                btnNo.Visibility = Visibility.Collapsed;
                btnYes.Visibility = Visibility.Visible;
                btnYes.Content = "Ok";
                break;

            case MessageBoxButtons.YesNo:
                btnCancel.Visibility = Visibility.Collapsed;
                btnNo.Visibility = Visibility.Visible;
                btnYes.Visibility = Visibility.Visible;
                break;

            case MessageBoxButtons.YesNoCancel:
                btnCancel.Visibility = Visibility.Visible;
                btnNo.Visibility = Visibility.Visible;
                btnYes.Visibility = Visibility.Visible;
                break;
        }
    }

    private void MessageBoxChildWindow_Closed(object sender, EventArgs e)
    {
        if (OnMessageBoxClosed != null)
            OnMessageBoxClosed(this.Result);
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        if (btnYes.Content.ToString().ToLower().Equals("yes") == true)
        {
            //yes button
            this.Result = MessageBoxResult.Yes;
        }
        else
        {
            //ok button
            this.Result = MessageBoxResult.OK;
        }

        this.Close();
    }

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

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

}

}

And you can call this Message Box

你可以称这个消息框

For Yes No Buttons

对于 Yes No 按钮

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Yes or No Message Box...!", SLMessageBox.MessageBoxButtons.YesNo, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

For Only Ok Button

仅用于确定按钮

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Ok Message Box...!", SLMessageBox.MessageBoxButtons.Ok, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

For Ok and Cancel Buttons

对于确定和取消按钮

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Ok and Cancel Message Box...!", SLMessageBox.MessageBoxButtons.OkCancel, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

Edit: And to check the result of the MessageBox what the user clicked

编辑:并检查用户单击的 MessageBox 的结果

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Yes, No and Cancel Buttons Message Box...!", SLMessageBox.MessageBoxButtons.YesNoCancel, , SLMessageBox.MessageBoxIcon.Information);
messBox.Show();
messBox.OnMessageBoxClosed += messBox_OnDeleteMessageBoxClosed;

private void messBox_OnDeleteMessageBoxClosed(MessageBoxResult result)
{
   if(result==MessageBoxResult.Yes)
   {
     //....
   }
   else if(result==MessageBoxResult.No)
   {
     //....
   }
   else
   {
     //...
   }
}

回答by Chris Shao

The API only exposes the OK/Cancel. you can use the Coding4Fun MessagePrompt from Coding4FunToolkitto create Yes/No message:

API 只公开确定/取消。您可以使用Coding4Fun MessagePrompt从Coding4FunToolkit创建是/否消息:

MessagePrompt msgPrompt = new MessagePrompt();
msgPrompt.Message = "Your Message.";
Button yesBtn = new Button() { Content = "Yes" };
yesBtn.Click += new RoutedEventHandler(yesBtn_Click);
Button noBtn = new Button() { Content = "No" };
noBtn.Click += new RoutedEventHandler(noBtn_Click);
msgPrompt.ActionPopUpButtons.Add(noBtn);
msgPrompt.ActionPopUpButtons.Add(yesBtn);
msgPrompt.Show();

回答by Sam

The API only exposes "OK" and "Cancel". If you need to use different button I'm afraid you'll have to use a custom message box.

API 只公开“OK”和“Cancel”。如果您需要使用不同的按钮,恐怕您将不得不使用自定义消息框。

E.g. http://shawnoster.com/2012/10/welcome-custommessagebox-to-the-windows-phone-toolkit/

例如http://shawnoster.com/2012/10/welcome-custommessagebox-to-the-windows-phone-toolkit/

http://blogs.microsoft.co.il/tomershamam/2010/10/19/windows-phone-7-custom-message-box/

http://blogs.microsoft.co.il/tomershamam/2010/10/19/windows-phone-7-custom-message-box/

回答by Pradeep Kesharwani

Another option is to use the Guide class from the XNA framework. There is a BeginShowMessageBox method giving you the option to pass in whatever strings you wish for the buttons.

另一种选择是使用 XNA 框架中的 Guide 类。有一个 BeginShowMessageBox 方法,您可以选择为按钮传递任何字符串。

follow following link

按照以下链接

link 1

链接 1

link 2

链接 2

link 3

链接 3

回答by Xela

No, MessageBox is a sealed type.

不,MessageBox 是密封类型。

You will need to make your own MessageBox control, that has "Yes" and "No" buttons.

您需要制作自己的 MessageBox 控件,该控件具有“是”和“否”按钮。

回答by Parag555

if (MessageBox.Show("Are you sure?", "Attention!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                //this block will be executed when Yes is selected
                MessageBox.Show("Data Deleted", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                //this block will be executed when No is selected
            }