Ejemplo n.º 1
0
        private static Button CreateCtrlButtonWithResult(CustomMeassageBox customMessageBox, string content, bool?dialogResult, bool isDefault = false)
        {
            var btn = new Button()
            {
                Style = customMessageBox.CtrlButtonStyle, Content = content, IsDefault = isDefault
            };
            CommandBinding binding = new CommandBinding(CustomButtonCommand.ButtonLeftClickCommand);

            binding.Executed += (o, e) => { MessageBoxBtn_Excuted(o, e, customMessageBox, dialogResult); };
            btn.CommandBindings.Add(binding);
            return(btn);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 由于原因,按cancel和no按钮,都会返回false
 /// </summary>
 /// <param name="o"></param>
 /// <param name="e"></param>
 /// <param name="customMessageBox"></param>
 /// <param name="dialogResult"></param>
 private static void MessageBoxBtn_Excuted(object o, ExecutedRoutedEventArgs e, CustomMeassageBox customMessageBox, bool?dialogResult)
 {
     if (dialogResult.HasValue)
     {
         customMessageBox.DialogResult = dialogResult;
     }
     else
     {
         customMessageBox.Close();
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 显示在指定的窗口前面的消息框。显示具有消息、标题栏说明、按钮的消息框;这接受一个默认消息框的结果并返回结果。
        /// </summary>
        /// <param name="owner">表示消息框的所有者窗口的 System.Windows.Window 。</param>
        /// <param name="messageBoxText">指定文本显示的 System.String 。</param>
        /// <param name="caption">指定标题栏声明中显示的 System.String 。</param>
        /// <param name="button">指定的 System.Windows.MessageBoxButton 值要显示的按钮或按钮。</param>
        /// <param name="defaultResult">指定消息框的默认结果的 System.Windows.MessageBoxResult 值。</param>
        /// <returns>指定的 System.Windows.MessageBoxResult 值哪个消息框按钮由用户单击。</returns>
        public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxResult defaultResult)
        {
            // MessageBox.Show();
            CustomMeassageBox customMessageBox = new CustomMeassageBox();

            customMessageBox.Owner   = owner;
            customMessageBox.Message = messageBoxText;
            customMessageBox.Title   = caption;
            if (owner != null)
            {
                customMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }

            switch (button)
            {
            case MessageBoxButton.OKCancel:
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "确定", true, defaultResult == MessageBoxResult.OK));
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "取消", null, defaultResult == MessageBoxResult.Cancel));
                break;

            case MessageBoxButton.YesNo:
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "是", true, defaultResult == MessageBoxResult.Yes));
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "否", false, defaultResult == MessageBoxResult.No));

                break;

            case MessageBoxButton.YesNoCancel:
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "是", true, defaultResult == MessageBoxResult.Yes));
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "否", false, defaultResult == MessageBoxResult.No));
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "取消", null, defaultResult == MessageBoxResult.Cancel));
                break;

            case MessageBoxButton.OK:
            default:
                customMessageBox.CtrlButtonCollection.Add(CreateCtrlButtonWithResult(customMessageBox, "确定", true, defaultResult == MessageBoxResult.OK));
                break;
            }
            bool?result = customMessageBox.ShowDialog();

            // 返回MessageBoxResult
            switch (button)
            {
            case MessageBoxButton.OKCancel:
            {
                return(result == true ? MessageBoxResult.OK : MessageBoxResult.Cancel);
            }

            case MessageBoxButton.YesNo:
            {
                return(result == true ? MessageBoxResult.Yes : MessageBoxResult.No);
            }

            case MessageBoxButton.YesNoCancel:
            {
                return(result == true ? MessageBoxResult.Yes : result == false ? MessageBoxResult.No : MessageBoxResult.Cancel);
            }

            case MessageBoxButton.OK:
            {
                return(result == true ? MessageBoxResult.OK : MessageBoxResult.Cancel);
            }

            default:
            {
                return(result == true ? MessageBoxResult.OK : MessageBoxResult.Cancel);
            }
            }
        }