- goalbook.zip
- GoalBook
- Dependencies
- CAG WPF 2
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.UnityExtensions.dll
- Microsoft.Practices.ObjectBuilder2.dll
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.dll
- vssver2.scc
- CSLA
- Csla.dll
- Csla.XmlSerializers.dll
- vssver2.scc
- Infragistics
- Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
- Infragistics3.Wpf.Editors.v9.1.Express.dll
- Infragistics3.Wpf.v9.1.Express.dll
- vssver2.scc
- WPFToolkit
- vssver2.scc
- WPFToolkit.dll
- GoalBook.Controls
- GoalBook.Goals
- GoalBook.Infrastructure
- GoalBook.Notes
- GoalBook.Public
- GoalBook.Shell
- App.ico
- App.xaml
- App.xaml.cs
- Bootstrapper.cs
- Commands
- GoalBook.Shell.csproj
- GoalBook.Shell.csproj.user
- Journal.ico
- Journal48.ico
- Misc
- Modules
- Properties
- Resources
- arrow_refresh.png
- arrow_undo.png
- checked.gif
- cross.png
- disk.png
- email.png
- erase.png
- error.png
- exclamation.png
- flag_red.png
- group.png
- help.png
- information.png
- magnifier.png
- page_red.png
- pencil.png
- printer.png
- user.png
- vssver2.scc
- Wave.jpg
- world.png
- world_link.png
- Services
- Splash.png
- Views
- vssver2.scc
- Windows
- GoalBook.sln
- GoalBook.snk
- GoalBook.Synchronisation
- GoalBook.Tasks
- License.txt
- ReadMe.txt
- SolutionInfo.cs
- vssver2.scc
|
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword.
// Credit to CompositeWPFContrib project for core functionality of Dialog window.
// http://www.codeplex.com/CompositeWPFContrib
//===============================================================================
#region Using Statements
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.Constants;
using GoalBook.Infrastructure.Enums;
using GoalBook.Infrastructure.Events;
using GoalBook.Infrastructure.Interfaces;
using GoalBook.Shell.Views;
#endregion
namespace GoalBook.Shell.Windows
{
/// <summary>
/// Provides a separate workspace for modal dialogs.
/// </summary>
public class DialogPresenter
{
private Dialog _dialogWindow;
/// <summary>
/// ShowView.
/// </summary>
/// <param name="owner"></param>
/// <param name="view"></param>
/// <param name="options"></param>
/// <returns></returns>
public bool? ShowView(Window owner, UserControl view, DialogOptions options)
{
// Initialise DialogWindow.
_dialogWindow = new Dialog();
_dialogWindow.View = view as DependencyObject;
_dialogWindow.Owner = owner;
// Apply the dialog options if provided
if (options != null) { SetDialogOptions(_dialogWindow, options); }
// Show dialog and return result.
bool? result = _dialogWindow.ShowDialog();
return _dialogWindow.IsCancelled ? null : result;
}
/// <summary>
/// ShowView.
/// </summary>
/// <param name="owner"></param>
/// <param name="view"></param>
/// <param name="options"></param>
/// <returns></returns>
public bool? ShowView(Window owner, UserControl view, DialogOptions options, Func<bool> validationAction)
{
// Initialise DialogWindow.
_dialogWindow = new Dialog();
_dialogWindow.View = view as DependencyObject;
_dialogWindow.Owner = owner;
//Initialise the ValidationAction.
_dialogWindow.ValidateDialog += new EventHandler<CancelEventArgs>((sender, e) =>
{
e.Cancel = false;
if (validationAction != null)
{
// Validate action will return false when validation fails
// invert this result to cancel the close action.
e.Cancel = !validationAction();
}
});
_dialogWindow.buttonAccept.IsEnabled = true;
// Apply the dialog options if provided
if (options != null) { SetDialogOptions(_dialogWindow, options); }
// Show dialog and return result.
bool? result = _dialogWindow.ShowDialog();
return _dialogWindow.IsCancelled ? null : result;
}
/// <summary>
/// Displays a view in the dialog workspace and returns the dialog result
/// after the dialog has been closed.
/// </summary>
public bool? ShowView(Window owner, IErrorInfoDialogView view, DialogOptions options, IDataErrorInfo errorInfo)
{
_dialogWindow = new Dialog();
_dialogWindow.View = view as DependencyObject;
_dialogWindow.Owner = owner;
//Subscribe to the view's ErrorInfoChanged event.
view.ErrorInfoChanged += new ErrorInfoChangedEventHandler(View_ErrorInfoChanged);
if (errorInfo.Error.Length > 0)
{
//Intialise the dialog interface (save
//button and error image) when error exists.
view.RaiseErrorInfoChanged(errorInfo);
}
// Subscribe to _dialogWindow.CanCloseDialog
_dialogWindow.CanCloseDialog += new EventHandler<CancelEventArgs>(DialogWindow_CanCloseDialog);
// Apply the dialog options if provided
if (options != null) { SetDialogOptions(_dialogWindow, options); }
// Show dialog and return result.
return _dialogWindow.ShowDialog();
}
/// <summary>
/// Displays a view in the dialog workspace and returns the dialog result
/// after the dialog has been closed.
/// </summary>
public bool? ShowView(Window owner, IErrorInfoDialogView view, DialogOptions options, Func<bool> validationAction, IDataErrorInfo errorInfo)
{
_dialogWindow = new Dialog();
_dialogWindow.View = view as DependencyObject;
_dialogWindow.Owner = owner;
//Initialise the ValidationAction.
_dialogWindow.ValidateDialog += new EventHandler<CancelEventArgs>((sender, e) =>
{
e.Cancel = false;
if (validationAction != null)
{
// Validate action will return false when validation fails
// invert this result to cancel the close action.
e.Cancel = !validationAction();
}
});
//Subscribe to the view's ErrorInfoChanged event.
view.ErrorInfoChanged += new ErrorInfoChangedEventHandler(View_ErrorInfoChanged);
if (errorInfo.Error.Length > 0)
{
//Intialise the dialog interface (save
//button and error image) when error exists.
view.RaiseErrorInfoChanged(errorInfo);
}
// Subscribe to _dialogWindow.CanCloseDialog
_dialogWindow.CanCloseDialog += new EventHandler<CancelEventArgs>(DialogWindow_CanCloseDialog);
// Apply the dialog options if provided
if (options != null) { SetDialogOptions(_dialogWindow, options); }
// Show dialog and return result.
return _dialogWindow.ShowDialog();
}
/// <summary>
/// Handle View_ErrorInfoChanged event. Implements the validation mechanism
/// for the Dialog window using IDataErrorInfo interface.
/// </summary>
private void View_ErrorInfoChanged(ErrorInfoChangedEventArgs e)
{
if (e.ErrorInfo == null) { return; }
if (this._dialogWindow == null) { return; }
_dialogWindow.IsDirty = true;
_dialogWindow.buttonAccept.IsEnabled = (e.ErrorInfo.Error.Length == 0);
_dialogWindow.imageValidation.Visibility = (e.ErrorInfo.Error.Length > 0 ? Visibility.Visible : Visibility.Hidden);
_dialogWindow.imageValidation.ToolTip = e.ErrorInfo.Error;
}
/// <summary>
/// Handle DialogWindow_CanCloseDialog event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DialogWindow_CanCloseDialog(object sender, CancelEventArgs e)
{
DialogOptions options = new DialogOptions();
options.AllowResize = false;
options.DialogTitle = Properties.Resources.ConfirmCancelCaption;
options.DialogMessage = Properties.Resources.ConfirmCancelMessage;
options.IconUri = new Uri(MenuConstants.MESSAGE_EXCLAMATION_IMAGE_URI);
options.Buttons = ButtonType.YesNo;
Mouse.SetCursor(Cursors.Wait);
MessageView view = new MessageView(options.DialogMessage);
Dialog dialogWindow = new Dialog();
dialogWindow.View = view as DependencyObject;
dialogWindow.Owner = _dialogWindow;
SetDialogOptions(dialogWindow, options);
// Show dialog and return result.
e.Cancel = !dialogWindow.ShowDialog().Value;
}
/// <summary>
/// Set Dialog Options.
/// </summary>
/// <param name="dialogWindow"></param>
/// <param name="options"></param>
private void SetDialogOptions(Dialog dialogWindow, DialogOptions options)
{
if (options == null) { return; }
dialogWindow.ResizeMode = ResizeMode.NoResize; //options.AllowResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize;
dialogWindow.Title = options.DialogTitle ?? string.Empty;
//Set dialog icon. If no uri is specified then use the app icon.
dialogWindow.Icon = options.IconUri == null ?
BitmapFrame.Create(new Uri(MenuConstants.APP_IMAGE_URI)) :
BitmapFrame.Create(options.IconUri);
switch (options.Buttons)
{
case ButtonType.OK:
dialogWindow.buttonAccept.Content = Properties.Resources.OKText;
dialogWindow.buttonAccept.Margin = new Thickness(10);
dialogWindow.buttonAccept.IsEnabled = true;
dialogWindow.buttonReject.Visibility =
dialogWindow.buttonCancel.Visibility = Visibility.Collapsed;
break;
case ButtonType.OKCancel:
dialogWindow.buttonAccept.Content = Properties.Resources.OKText;
dialogWindow.buttonCancel.Focus();
dialogWindow.buttonReject.Visibility = Visibility.Collapsed;
break;
case ButtonType.YesNo:
dialogWindow.buttonAccept.Content = Properties.Resources.YesText;
dialogWindow.buttonAccept.IsEnabled = true;
dialogWindow.buttonReject.Focus();
dialogWindow.buttonReject.Visibility = Visibility.Visible;
dialogWindow.buttonCancel.Visibility = Visibility.Collapsed;
break;
case ButtonType.YesNoCancel:
dialogWindow.buttonAccept.Content = Properties.Resources.YesText;
dialogWindow.buttonAccept.IsEnabled = true;
dialogWindow.buttonAccept.Focus();
dialogWindow.buttonReject.Visibility = Visibility.Visible;
break;
}
if (options.DialogSize.Width != 0 && options.DialogSize.Height != 0)
{
dialogWindow.Width = dialogWindow.MinWidth = options.DialogSize.Width;
dialogWindow.Height = dialogWindow.MinHeight = options.DialogSize.Height;
dialogWindow.SizeToContent = SizeToContent.Manual;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.