5 using System.Windows.Input;
6 using System.Windows.Interactivity;
8 namespace SiliconStudio.Presentation.Behaviors
19 public static readonly DependencyProperty DialogResultProperty = DependencyProperty.Register(
"DialogResult", typeof(
bool?), typeof(
CloseWindowBehavior<T>));
24 public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(
CloseWindowBehavior<T>),
new PropertyMetadata(null, CommandChanged));
29 public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
"CommandParameter", typeof(
object), typeof(
CloseWindowBehavior<T>),
new PropertyMetadata(null, CommandParameterChanged));
34 public bool?
DialogResult {
get {
return (
bool?)GetValue(DialogResultProperty); } set { SetValue(DialogResultProperty, value); } }
39 public ICommand Command {
get {
return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
44 public object CommandParameter {
get {
return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } }
52 AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, Command.CanExecute(CommandParameter));
56 private static void CommandChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
59 var oldCommand = e.OldValue as ICommand;
60 var newCommand = e.NewValue as ICommand;
62 if (oldCommand != null)
64 oldCommand.CanExecuteChanged -= behavior.CommandCanExecuteChanged;
66 if (newCommand != null)
68 newCommand.CanExecuteChanged += behavior.CommandCanExecuteChanged;
72 private static void CommandParameterChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
74 var behavior = (ButtonCloseWindowBehavior)d;
75 if (behavior.Command != null)
77 behavior.AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, behavior.Command.CanExecute(behavior.CommandParameter));
81 private void CommandCanExecuteChanged(
object sender,
EventArgs e)
83 AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, Command.CanExecute(CommandParameter));
91 if (Command != null && Command.CanExecute(CommandParameter))
93 Command.Execute(CommandParameter);
96 var window = Window.GetWindow(AssociatedObject);
97 if (window == null)
throw new InvalidOperationException(
"The button attached to this behavior is not in a window");
100 bool dialogResultUpdated =
false;
105 dialogResultUpdated =
true;
A base behavior that will close the window it is contained in an event occurs on a control...
DialogResult
An enum representing the result of a dialog invocation.
override void OnAttached()
void Close()
Invokes the command and close the containing window.