自定义WPF MessageBox未返回结果

时间:2014-10-23 10:45:15

标签: c# wpf xaml

我已按照(here)教程使用MessageBoxes创建自定义WPF。我有一个小问题,因为目前我的自定义框不会返回MessageBoxResult。我不明白为什么会这样。

以下是xaml文件PDSAMessageBoxView

 <Window x:Class="Resources.PDSAMessageBoxView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="NoResize"
    ShowInTaskbar="True"
    FontFamily="Segoe UI"
    WindowStartupLocation="CenterScreen"
    Height="300"
    Width="420"
     >
 <Border>
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
     <TextBlock Name="tbMessage"
     Text="Message goes here..."
     TextWrapping="Wrap" />
        <StackPanel Grid.Row="1"
                    >
            <Button Content="Yes"
                     x:Name="btnYes"
                     Click="btnYes_Click" />
            <Button Content="No"
                     x:Name="btnNo"
                     Click="btnNo_Click" />
            <Button Content="OK"
                     x:Name="btnOk"
                     Click="btnOk_Click" />
            <Button Content="Cancel"
                     x:Name="btnCancel"
                     Click="btnCancel_Click" />
        </StackPanel>
    </Grid>
</Border>
</Window>

这是相应的PDSAMessageBoxView.xaml.cs文件:

public partial class PDSAMessageBoxView : Window
{
    public PDSAMessageBoxView()
    {
        InitializeComponent();
    }

    public static MessageBoxResult Show(string message)
    {
        return Show(message, string.Empty, MessageBoxButton.OK);
    }

    public static MessageBoxResult Show(string message,
      string caption)
    {
        return Show(message, caption, MessageBoxButton.OK);
    }

    public static MessageBoxResult Show(string message,
      string caption, MessageBoxButton buttons)
    {
        MessageBoxResult result = MessageBoxResult.None;
        PDSAMessageBoxView dialog = new PDSAMessageBoxView();

        dialog.Title = caption;
        dialog.tbMessage.Text = message;
        dialog.Buttons = buttons;
        // If just an OK button, allow the user to just 
        // move away from the dialog
        if (buttons == MessageBoxButton.OK)
            dialog.Show();
        else
        {
            dialog.ShowDialog();
            result = dialog.Result;
        }

        return result;
    }




    public MessageBoxButton Buttons { get; set; }

    public MessageBoxResult Result { get; set; }

    private void btnYes_Click(object sender, RoutedEventArgs e)
    {

    }

    private void btnNo_Click(object sender, RoutedEventArgs e)
    {

    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {

    }

    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {

    }
}

最后,代码被调用如下:

MessageBoxResult result = Resources.PDSAMessageBoxView.Show("Are you sure you want to exit?", "Confirm Shutdown",
            System.Windows.MessageBoxButton.YesNo);

返回的结果始终为None。此外,显示的按钮始终为yesnocancelok,它们应仅为YesNo

如果有人能指出我正确的方向,我会非常感激。

1 个答案:

答案 0 :(得分:5)

要:

  

返回的结果始终为None。并且   显示的按钮总是是,否,取消和确定,当它们应该只是是和否。

你很简单忘了实现这个功能。当此(public MessageBoxButton Buttons { get; set; })属性发生更改时,您必须隐藏并显示按钮。

接下来,您忘记在按钮单击方法::

中设置结果
 private void btnYes_Click(object sender, RoutedEventArgs e)
    {

    }

    private void btnNo_Click(object sender, RoutedEventArgs e)
    {

    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {

    }

你没有完成所有的事情,就是这样。

这是一个正确的实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MessageBoxHelp
{
    public partial class PDSAMessageBoxView : Window
    {
        private MessageBoxButton buttons;

        public PDSAMessageBoxView()
        {
            InitializeComponent();
        }

        public static MessageBoxResult Show(string message)
        {
            return Show(message, string.Empty, MessageBoxButton.OK);
        }

        public static MessageBoxResult Show(string message,
          string caption)
        {
            return Show(message, caption, MessageBoxButton.OK);
        }

        public static MessageBoxResult Show(string message,
          string caption, MessageBoxButton buttons)
        {
            MessageBoxResult result = MessageBoxResult.None;
            PDSAMessageBoxView dialog = new PDSAMessageBoxView();

            dialog.Title = caption;
            dialog.tbMessage.Text = message;
            dialog.Buttons = buttons;
            // If just an OK button, allow the user to just 
            // move away from the dialog
            if (buttons == MessageBoxButton.OK)
                dialog.Show();
            else
            {
                dialog.ShowDialog();
                result = dialog.Result;
            }

            return result;
        }




        public MessageBoxButton Buttons
        {
            get
            {
                return buttons;
            }

            set
            {
                buttons = value;

                btnCancel.Visibility = Visibility.Collapsed;
                btnOk.Visibility = Visibility.Collapsed;
                btnYes.Visibility = Visibility.Collapsed;
                btnNo.Visibility = Visibility.Collapsed;

                switch (buttons)
                {
                    case MessageBoxButton.OK:
                        btnOk.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.OKCancel:
                        btnCancel.Visibility = Visibility.Visible;
                        btnOk.Visibility = Visibility.Visible;
                        break;

                    case MessageBoxButton.YesNo:
                        btnYes.Visibility = Visibility.Visible;
                        btnNo.Visibility = Visibility.Visible;
                        break;

                    case MessageBoxButton.YesNoCancel:
                        btnCancel.Visibility = Visibility.Visible;
                        break;
                }
            }
        }

        public MessageBoxResult Result { get; set; }

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

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

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

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

我已将您的命名空间更改为MessageBoxHelp,因为在我的情况下,资源是一个糟糕的选择。