You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4C/GummingCommon/Dialogs/MessageBoxServiceViewModule.cs

580 lines
18 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Documents;
using System.Threading;
using System.Diagnostics;
using GummingCommon;
namespace GummingCommon
{
public class MessageBoxServiceViewModule : IMessageBoxService, INotifyPropertyChanged
{
public MessageBoxServiceView View { get; set; }
public bool IsKeepInProcessVisibility { get; set; }
#region constructor
public MessageBoxServiceViewModule()
{
CancelCommand = new DelegateCommand(this.btCancelX_Click);
IsKeepInProcessVisibility = false;
View = new MessageBoxServiceView();
View.DataContext = this;
WindowTitle = "";
View.Topmost = true;
}
public MessageBoxServiceViewModule(string lm, string cm, string rm)
{
this.btLeftContent = lm;
this.btCenterContent = cm;
this.btRightContent = rm;
CancelCommand = new DelegateCommand(this.btCancelX_Click);
IsKeepInProcessVisibility = false;
View = new MessageBoxServiceView();
View.DataContext = this;
WindowTitle = "";
View.Topmost = true;
}
#endregion
#region destructor
~MessageBoxServiceViewModule()
{
_self = null;
}
#endregion
#region Binding Properties
public string WindowTitle { get; private set; }
private Visibility _spWarningVisibility;
public Visibility spWarningVisibility
{
get
{
return _spWarningVisibility;
}
set
{
_spWarningVisibility = value;
OnPropertyChanged("spWarningVisibility");
}
}
private Visibility _spErrorVisibility;
public Visibility spErrorVisibility
{
get
{
return _spErrorVisibility;
}
set
{
_spErrorVisibility = value;
OnPropertyChanged("spErrorVisibility");
}
}
private Visibility _spInforVisibility;
public Visibility spInforVisibility
{
get
{
return _spInforVisibility;
}
set
{
_spInforVisibility = value;
OnPropertyChanged("spInforVisibility");
}
}
private Visibility _btLeftVisibility;
public Visibility btLeftVisibility
{
get
{
return _btLeftVisibility;
}
set
{
_btLeftVisibility = value;
OnPropertyChanged("btLeftVisibility");
}
}
private Visibility _btCenterVisibility;
public Visibility btCenterVisibility
{
get
{
return _btCenterVisibility;
}
set
{
_btCenterVisibility = value;
OnPropertyChanged("btCenterVisibility");
}
}
private Visibility _btRightVisibility;
public Visibility btRightVisibility
{
get
{
return _btRightVisibility;
}
set
{
_btRightVisibility = value;
OnPropertyChanged("btRightVisibility");
}
}
private object _btLeftContent;
public object btLeftContent
{
get
{
return _btLeftContent;
}
set
{
_btLeftContent = value;
OnPropertyChanged("btLeftContent");
}
}
private object _btCenterContent;
public object btCenterContent
{
get
{
return _btCenterContent;
}
set
{
_btCenterContent = value;
OnPropertyChanged("btCenterContent");
}
}
private object _btRightContent;
public object btRightContent
{
get
{
return _btRightContent;
}
set
{
_btRightContent = value;
OnPropertyChanged("btRightContent");
}
}
/// <summary>
///
/// </summary>
public bool Topmost { get; set; }
/// <summary>
///
/// </summary>
private string _Message;
public string Message
{
get
{
return _Message;
}
set
{
_Message = value;
OnPropertyChanged("Message");
}
}
private bool _LeftButtonIsDefault;
public bool LeftButtonIsDefault
{
get { return _LeftButtonIsDefault; }
set
{
_LeftButtonIsDefault = value;
OnPropertyChanged("LeftButtonIsDefault");
}
}
private bool _CenterButtonIsDefault;
public bool CenterButtonIsDefault
{
get { return _CenterButtonIsDefault; }
set
{
_CenterButtonIsDefault = value;
OnPropertyChanged("CenterButtonIsDefault");
}
}
private bool _RightButtonIsDefault;
public bool RightButtonIsDefault
{
get { return _RightButtonIsDefault; }
set
{
_RightButtonIsDefault = value;
OnPropertyChanged("RightButtonIsDefault");
}
}
#endregion
#region Binding Commands
private ICommand _HyperLinkClicked;
public ICommand HyperLinkClicked
{
get
{
return _HyperLinkClicked;
}
set
{
_HyperLinkClicked = value;
OnPropertyChanged("HyperLinkClicked");
}
}
private ICommand _CancelCommand;
public ICommand CancelCommand
{
get
{
return _CancelCommand;
}
set
{
_CancelCommand = value;
OnPropertyChanged("CancelCommand");
}
}
private ICommand _LeftButtonCommand;
public ICommand LeftButtonCommand
{
get { return this._LeftButtonCommand; }
set
{
this._LeftButtonCommand = value;
OnPropertyChanged("LeftButtonCommand");
}
}
private ICommand _CenterButtonCommand;
public ICommand CenterButtonCommand
{
get { return this._CenterButtonCommand; }
set
{
this._CenterButtonCommand = value;
OnPropertyChanged("CenterButtonCommand");
}
}
private ICommand _RightButtonCommand;
public ICommand RightButtonCommand
{
get { return this._RightButtonCommand; }
set
{
this._RightButtonCommand = value;
OnPropertyChanged("RightButtonCommand");
}
}
#endregion
#region IMessageBoxService members
private void ClearStatusDefaultCancel()
{
_LeftButtonIsDefault = false;
_CenterButtonIsDefault = false;
_RightButtonIsDefault = false;
}
/// <summary>
/// Gets or sets the state of the dialog.
/// </summary>
/// <value>The state of the dialog.</value>
public DialogState DialogState { get; set; }
/// <summary>
/// Gets or sets the value of the CanMuchOpened.
/// </summary>
public bool CanMuchOpened { get; set; }
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowError(string message, Window owner)
{
View.Owner = owner;
DialogState = DialogState.Cancel;
ShowMessage(message, "Error", CustomDialogIcons.Error);
}
public void ShowError(string message)
{
DialogState = DialogState.Cancel;
ShowMessage(message, "Error", CustomDialogIcons.Error);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowInformation(string message)
{
DialogState = DialogState.Cancel;
ShowMessage(message, "Information", CustomDialogIcons.Information);
}
/// <summary>
/// Shows an Successful message
/// </summary>
/// <param name="message">The Success Message</param>
public void ShowSuccess(string message)
{
DialogState = DialogState.Cancel;
ShowMessage(message, "Successful", CustomDialogIcons.OK);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowWarning(string message)
{
DialogState = DialogState.Cancel;
ShowMessage(message, "Warning", CustomDialogIcons.Warning);
}
/// <summary>
/// Displays a Yes/No dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public void ShowYesNo(string message, CustomDialogIcons icon)
{
DialogState = DialogState.Cancel;
this.Message = message;
SetImage(icon);
this.btLeftVisibility = Visibility.Hidden;
this.btCenterVisibility = Visibility.Visible;
this.btRightVisibility = Visibility.Visible;
this.btCenterContent = "确定";
this.btRightContent = "返回";
//this.btCenter.Focus();
ClearStatusDefaultCancel();
CenterButtonIsDefault = true;
this.CenterButtonCommand = new DelegateCommand(btOk_Click);
this.RightButtonCommand = new DelegateCommand(btNo_Click);
View.ShowDialog();
}
/// <summary>
/// Displays a Yes/No/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public void ShowYesNoCancel(string message, CustomDialogIcons icon)
{
DialogState = DialogState.Cancel;
this.Message = message;
SetImage(icon);
this.btLeftVisibility = Visibility.Visible;
this.btCenterVisibility = Visibility.Visible;
this.btRightVisibility = Visibility.Visible;
if (this.btLeftContent == null)
{
this.btLeftContent = "确定";
this.btCenterContent = "返回";
this.btRightContent = "取消";
}
ClearStatusDefaultCancel();
LeftButtonIsDefault = true;
this.LeftButtonCommand = new DelegateCommand(btOk_Click);
this.CenterButtonCommand = new DelegateCommand(btNo_Click);
this.RightButtonCommand = new DelegateCommand(btCancel_Click);
View.ShowDialog();
}
/// <summary>
/// Displays a OK/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public void ShowOkCancel(string message, CustomDialogIcons icon)
{
DialogState = DialogState.Cancel;
this.Message = message;
SetImage(icon);
this.btLeftVisibility = Visibility.Hidden;
this.btCenterVisibility = Visibility.Visible;
this.btRightVisibility = Visibility.Visible;
this.btCenterContent = "确定";
this.btRightContent = "取消";
//this.btCenter.Focus();
ClearStatusDefaultCancel();
CenterButtonIsDefault = true;
this.CenterButtonCommand = new DelegateCommand(btOk_Click);
this.RightButtonCommand = new DelegateCommand(btNo_Click);
View.ShowDialog();
}
private IMessageBoxService _self;
public IMessageBoxService New()
{
_self = new MessageBoxServiceViewModule();
DialogState = DialogState.Cancel;
return _self;
}
public bool ShowInTaskBar
{
get { return View.ShowInTaskbar; }
set { View.ShowInTaskbar = value; }
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Private Methods
/// <summary>
/// Shows a standard System.Windows.MessageBox using the parameters requested
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="heading">The heading to be displayed</param>
/// <param name="icon">The icon to be displayed.</param>
private void ShowMessage(string message, string heading, CustomDialogIcons icon)
{
this.Message = message;
SetImage(icon);
this.btLeftVisibility = Visibility.Hidden;
this.btCenterVisibility = Visibility.Hidden;
this.btRightVisibility = Visibility.Visible;
this.btRightContent = "确定";
//this.btRight.Focus();
ClearStatusDefaultCancel();
RightButtonIsDefault = true;
View.Topmost = Topmost;
this.RightButtonCommand = new DelegateCommand(btOk_Click);
View.ShowDialog();
}
/// <summary>
/// Translates a CustomDialogIcons into a standard WPF System.Windows.MessageBox MessageBoxImage.
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>A standard WPF System.Windows.MessageBox MessageBoxImage</returns>
private void SetImage(CustomDialogIcons icon)
{
switch (icon)
{
case CustomDialogIcons.Information:
this.spWarningVisibility = Visibility.Hidden;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Visible;
break;
case CustomDialogIcons.Question:
this.spWarningVisibility = Visibility.Hidden;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Visible;
break;
case CustomDialogIcons.Exclamation:
this.spWarningVisibility = Visibility.Hidden;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Visible;
break;
case CustomDialogIcons.Stop:
this.spWarningVisibility = Visibility.Visible;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Hidden;
break;
case CustomDialogIcons.Warning:
this.spWarningVisibility = Visibility.Visible;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Hidden;
break;
case CustomDialogIcons.Error:
this.spWarningVisibility = Visibility.Hidden;
this.spErrorVisibility = Visibility.Visible;
this.spInforVisibility = Visibility.Hidden;
break;
case CustomDialogIcons.OK:
this.spWarningVisibility = Visibility.Hidden;
this.spErrorVisibility = Visibility.Hidden;
this.spInforVisibility = Visibility.Visible;
break;
}
}
private void btOk_Click(object sender)
{
DialogState = DialogState.Ok;
View.DialogResult = true;
}
private void btNo_Click(object sender)
{
DialogState = DialogState.No;
View.DialogResult = true;
}
private void btCancel_Click(object sender)
{
DialogState = DialogState.Cancel;
View.DialogResult = true;
}
private void btCancelX_Click(object sender)
{
DialogState = DialogState.CancelX;
View.DialogResult = true;
}
#endregion
}
}