using IntegratedManagementConsole.Commons;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace IntegratedManagementConsole.Helper
{
///
/// Command 실행 후 DialogResult를 설정해주는 UI종속 클래스
///
public class DialogResultCommand : DependencyObject
{
private static ICommand _acceptButtonCommand;
private static object _acceptButtonCommandParameter;
private static ICommand _cancelButtonCommand;
private static object _cancelButtonCommandParameter;
public static readonly DependencyProperty AcceptButtonCommandProperty =
DependencyProperty.RegisterAttached("AcceptButtonCommand", typeof(ICommand),
typeof(DialogResultCommand),
new FrameworkPropertyMetadata(OnAcceptButtonCommandPropertyChanged));
public static readonly DependencyProperty AcceptButtonCommandParameterProperty =
DependencyProperty.RegisterAttached("AcceptButtonCommandParameter", typeof(object),
typeof(DialogResultCommand), new FrameworkPropertyMetadata(OnAcceptButtonCommandParameterPropertyChanged));
public static readonly DependencyProperty CancelButtonCommandProperty =
DependencyProperty.RegisterAttached("CancelButtonCommand", typeof(ICommand),
typeof(DialogResultCommand), new FrameworkPropertyMetadata(OnCancelButtonCommandPropertyChanged));
public static readonly DependencyProperty CancelButtonCommandParameterProperty =
DependencyProperty.RegisterAttached("CancelButtonCommandParameter", typeof(object),
typeof(DialogResultCommand), new FrameworkPropertyMetadata(OnCancelButtonCommandParameterPropertyChanged));
public static readonly DependencyProperty IsAcceptButtonProperty =
DependencyProperty.RegisterAttached("IsAcceptButton", typeof(bool),
typeof(DialogResultCommand), new FrameworkPropertyMetadata(OnIsAcceptButtonPropertyChanged));
public static readonly DependencyProperty IsCancelButtonProperty =
DependencyProperty.RegisterAttached("IsCancelButton", typeof(bool),
typeof(DialogResultCommand), new FrameworkPropertyMetadata(OnIsCancelButtonPropertyChanged));
// AcceptButtonCommandProperty
public static void SetAcceptButtonCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(AcceptButtonCommandProperty, value);
}
public static ICommand GetAcceptButtonCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(AcceptButtonCommandProperty);
}
// AcceptButtonCommandParameterProperty
public static void SetAcceptButtonCommandParameter(DependencyObject obj, object value)
{
obj.SetValue(AcceptButtonCommandParameterProperty, value);
}
public static object GetAcceptButtonCommandParameter(DependencyObject obj)
{
return (object)obj.GetValue(AcceptButtonCommandParameterProperty);
}
// CancelButtonCommandProperty
public static void SetCancelButtonCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CancelButtonCommandProperty, value);
}
public static ICommand GetCancelButtonCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CancelButtonCommandProperty);
}
// CancelButtonCommandParameterProperty
public static void SetCancelButtonCommandParameter(DependencyObject obj, object value)
{
obj.SetValue(CancelButtonCommandParameterProperty, value);
}
public static object GetCancelButtonCommandParameter(DependencyObject obj)
{
return (object)obj.GetValue(CancelButtonCommandParameterProperty);
}
// IsAcceptButtonProperty
public static void SetIsAcceptButton(DependencyObject obj, bool value)
{
obj.SetValue(IsAcceptButtonProperty, value);
}
public static bool GetIsAcceptButton(DependencyObject obj)
{
return (bool)obj.GetValue(IsAcceptButtonProperty);
}
// IsCancelButtonProperty
public static void SetIsCancelButton(DependencyObject obj, bool value)
{
obj.SetValue(IsCancelButtonProperty, value);
}
public static bool GetIsCancelButton(DependencyObject obj)
{
return (bool)obj.GetValue(IsCancelButtonProperty);
}
private static void OnIsAcceptButtonPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Button button = sender as Button;
if (button != null)
{
if ((bool)e.NewValue)
{
SetAcceptButton(button);
}
else
{
ResetAcceptButton(button);
}
}
}
private static void OnIsCancelButtonPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Button button = sender as Button;
if (button != null)
{
if ((bool)e.NewValue)
{
SetCancelButton(button);
}
else
{
ResetCancelButton(button);
}
}
}
private static void OnAcceptButtonCommandPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
_acceptButtonCommand = e.NewValue as ICommand;
}
private static void OnAcceptButtonCommandParameterPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
_acceptButtonCommandParameter = e.NewValue as object;
}
private static void OnCancelButtonCommandPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
_cancelButtonCommand = e.NewValue as ICommand;
}
private static void OnCancelButtonCommandParameterPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
_cancelButtonCommandParameter = e.NewValue as object;
}
private static void SetAcceptButton(Button button)
{
Window window = Window.GetWindow(button);
button.Command = new RelayCommand(new Action<object>(ExecuteAccept), new Predicate<object>(CanAcceptButtonExecute));
button.CommandParameter = window;
}
private static void ResetAcceptButton(Button button)
{
button.Command = null;
button.CommandParameter = null;
}
private static void ExecuteAccept(object buttonWindow)
{
if (_acceptButtonCommand != null) _acceptButtonCommand.Execute(_acceptButtonCommandParameter);
Window window = (Window)buttonWindow;
window.DialogResult = true;
}
private static void SetCancelButton(Button button)
{
Window window = Window.GetWindow(button);
button.Command = new RelayCommand(new Action<object>(ExecuteCancel), new Predicate<object>(CanCancelButtonExecute));
button.CommandParameter = window;
}
private static void ResetCancelButton(Button button)
{
button.Command = null;
button.CommandParameter = null;
}
private static void ExecuteCancel(object buttonWindow)
{
if (_cancelButtonCommand != null) _cancelButtonCommand.Execute(_cancelButtonCommandParameter);
Window window = (Window)buttonWindow;
window.DialogResult = false;
}
private static bool CanAcceptButtonExecute(object param)
{
if (_acceptButtonCommand == null)
{
return true;
}
return (_acceptButtonCommand.CanExecute(_acceptButtonCommandParameter));
}
private static bool CanCancelButtonExecute(object param)
{
if (_cancelButtonCommand == null)
{
return true;
}
return (_cancelButtonCommand.CanExecute(_cancelButtonCommandParameter));
}
}
}