Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CloseWindowBehavior.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Windows;
5 using System.Windows.Input;
6 using System.Windows.Interactivity;
7 
8 namespace SiliconStudio.Presentation.Behaviors
9 {
10  /// <summary>
11  /// A base behavior that will close the window it is contained in an event occurs on a control. A command can be executed
12  /// before closing the window by using the <see cref="Command"/> and <see cref="CommandParameter"/> properties of this behavior.
13  /// </summary>
14  public abstract class CloseWindowBehavior<T> : Behavior<T> where T : DependencyObject
15  {
16  /// <summary>
17  /// Identifies the <see cref="DialogResult"/> dependency property.
18  /// </summary>
19  public static readonly DependencyProperty DialogResultProperty = DependencyProperty.Register("DialogResult", typeof(bool?), typeof(CloseWindowBehavior<T>));
20 
21  /// <summary>
22  /// Identifies the <see cref="Command"/> dependency property.
23  /// </summary>
24  public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CloseWindowBehavior<T>), new PropertyMetadata(null, CommandChanged));
25 
26  /// <summary>
27  /// Identifies the <see cref="CommandParameter"/> dependency property.
28  /// </summary>
29  public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CloseWindowBehavior<T>), new PropertyMetadata(null, CommandParameterChanged));
30 
31  /// <summary>
32  /// Gets or sets the value to set to the <see cref="Window.DialogResult"/> property of the window the associated button is contained in.
33  /// </summary>
34  public bool? DialogResult { get { return (bool?)GetValue(DialogResultProperty); } set { SetValue(DialogResultProperty, value); } }
35 
36  /// <summary>
37  /// Gets or sets the command to execute before closing the window.
38  /// </summary>
39  public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
40 
41  /// <summary>
42  /// Gets or sets the parameter of the command to execute before closing the window.
43  /// </summary>
44  public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } }
45 
46  /// <inheritdoc/>
47  protected override void OnAttached()
48  {
49  base.OnAttached();
50  if (Command != null)
51  {
52  AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, Command.CanExecute(CommandParameter));
53  }
54  }
55 
56  private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
57  {
58  var behavior = (ButtonCloseWindowBehavior)d;
59  var oldCommand = e.OldValue as ICommand;
60  var newCommand = e.NewValue as ICommand;
61 
62  if (oldCommand != null)
63  {
64  oldCommand.CanExecuteChanged -= behavior.CommandCanExecuteChanged;
65  }
66  if (newCommand != null)
67  {
68  newCommand.CanExecuteChanged += behavior.CommandCanExecuteChanged;
69  }
70  }
71 
72  private static void CommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
73  {
74  var behavior = (ButtonCloseWindowBehavior)d;
75  if (behavior.Command != null)
76  {
77  behavior.AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, behavior.Command.CanExecute(behavior.CommandParameter));
78  }
79  }
80 
81  private void CommandCanExecuteChanged(object sender, EventArgs e)
82  {
83  AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, Command.CanExecute(CommandParameter));
84  }
85 
86  /// <summary>
87  /// Invokes the command and close the containing window.
88  /// </summary>
89  protected void Close()
90  {
91  if (Command != null && Command.CanExecute(CommandParameter))
92  {
93  Command.Execute(CommandParameter);
94  }
95 
96  var window = Window.GetWindow(AssociatedObject);
97  if (window == null) throw new InvalidOperationException("The button attached to this behavior is not in a window");
98 
99  // Window.DialogResult setter will throw an exception when the window was not displayed with ShowDialog, even if we're setting null.
100  bool dialogResultUpdated = false;
101  if (DialogResult != window.DialogResult)
102  {
103  // Setting DialogResult to a non-null value will close the window, we don't want to invoke Close after that.
104  window.DialogResult = DialogResult;
105  dialogResultUpdated = true;
106  }
107 
108  if (DialogResult == null || !dialogResultUpdated)
109  {
110  window.Close();
111  }
112  }
113  }
114 }
A base behavior that will close the window it is contained in an event occurs on a control...
A behavior that can be attached to a ButtonBase and will close the window it is contained in when cli...
DialogResult
An enum representing the result of a dialog invocation.
Definition: DialogResult.cs:9
void Close()
Invokes the command and close the containing window.