chore: use file-scoped namespace

This commit is contained in:
NaBian committed 2022-03-13 20:28:58 +08:00
1 parent 99d2fafb72
commit bbf1b7d4c6
748 files changed
+57929 -58678

No files matched your search

@@ -27,169 +27,168 @@ using System.Windows;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Interactivity
namespace HandyControl.Interactivity;
public class EventToCommand : TriggerAction<DependencyObject>
{
public class EventToCommand : TriggerAction<DependencyObject>
public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
public const string AlwaysInvokeCommandPropertyName = "AlwaysInvokeCommand";
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
nameof(CommandParameter), typeof(object), typeof(EventToCommand), new PropertyMetadata(null,
(s, e) =>
{
var eventToCommand = s as EventToCommand;
if (eventToCommand?.AssociatedObject == null)
return;
eventToCommand.EnableDisableElement();
}));
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(EventToCommand),
new PropertyMetadata(null, (s, e) => OnCommandChanged(s as EventToCommand, e)));
public static readonly DependencyProperty MustToggleIsEnabledProperty = DependencyProperty.Register(
nameof(MustToggleIsEnabled), typeof(bool), typeof(EventToCommand), new PropertyMetadata(ValueBoxes.FalseBox,
(s, e) =>
{
var eventToCommand = s as EventToCommand;
if (eventToCommand?.AssociatedObject == null)
return;
eventToCommand.EnableDisableElement();
}));
public static readonly DependencyProperty EventArgsConverterParameterProperty =
DependencyProperty.Register(nameof(EventArgsConverterParameter), typeof(object),
typeof(EventToCommand), new PropertyMetadata(null));
public static readonly DependencyProperty AlwaysInvokeCommandProperty =
DependencyProperty.Register(nameof(AlwaysInvokeCommand), typeof(bool), typeof(EventToCommand),
new PropertyMetadata(ValueBoxes.FalseBox));
private object _commandParameterValue;
private bool? _mustToggleValue;
public ICommand Command
{
public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
get => (ICommand) GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public const string AlwaysInvokeCommandPropertyName = "AlwaysInvokeCommand";
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
nameof(CommandParameter), typeof(object), typeof(EventToCommand), new PropertyMetadata(null,
(s, e) =>
{
var eventToCommand = s as EventToCommand;
if (eventToCommand?.AssociatedObject == null)
return;
eventToCommand.EnableDisableElement();
}));
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(EventToCommand),
new PropertyMetadata(null, (s, e) => OnCommandChanged(s as EventToCommand, e)));
public static readonly DependencyProperty MustToggleIsEnabledProperty = DependencyProperty.Register(
nameof(MustToggleIsEnabled), typeof(bool), typeof(EventToCommand), new PropertyMetadata(ValueBoxes.FalseBox,
(s, e) =>
{
var eventToCommand = s as EventToCommand;
if (eventToCommand?.AssociatedObject == null)
return;
eventToCommand.EnableDisableElement();
}));
public static readonly DependencyProperty EventArgsConverterParameterProperty =
DependencyProperty.Register(nameof(EventArgsConverterParameter), typeof(object),
typeof(EventToCommand), new PropertyMetadata(null));
public static readonly DependencyProperty AlwaysInvokeCommandProperty =
DependencyProperty.Register(nameof(AlwaysInvokeCommand), typeof(bool), typeof(EventToCommand),
new PropertyMetadata(ValueBoxes.FalseBox));
private object _commandParameterValue;
private bool? _mustToggleValue;
public ICommand Command
public object CommandParameterValue
{
get => _commandParameterValue ?? CommandParameter;
set
{
get => (ICommand) GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
public object CommandParameterValue
{
get => _commandParameterValue ?? CommandParameter;
set
{
_commandParameterValue = value;
EnableDisableElement();
}
}
public bool MustToggleIsEnabled
{
get => (bool) GetValue(MustToggleIsEnabledProperty);
set => SetValue(MustToggleIsEnabledProperty, ValueBoxes.BooleanBox(value));
}
public bool MustToggleIsEnabledValue
{
get
{
if (_mustToggleValue.HasValue)
return _mustToggleValue.Value;
return MustToggleIsEnabled;
}
set
{
_mustToggleValue = value;
EnableDisableElement();
}
}
public bool PassEventArgsToCommand { get; set; }
public IEventArgsConverter EventArgsConverter { get; set; }
public object EventArgsConverterParameter
{
get => GetValue(EventArgsConverterParameterProperty);
set => SetValue(EventArgsConverterParameterProperty, value);
}
public bool AlwaysInvokeCommand
{
get => (bool) GetValue(AlwaysInvokeCommandProperty);
set => SetValue(AlwaysInvokeCommandProperty, ValueBoxes.BooleanBox(value));
}
protected override void OnAttached()
{
base.OnAttached();
_commandParameterValue = value;
EnableDisableElement();
}
private FrameworkElement GetAssociatedObject() => AssociatedObject as FrameworkElement;
private ICommand GetCommand() => Command;
public void Invoke() => Invoke(null);
protected override void Invoke(object parameter)
{
if (AssociatedElementIsDisabled() && !AlwaysInvokeCommand)
return;
var command = GetCommand();
var parameter1 = CommandParameterValue;
if (parameter1 == null && PassEventArgsToCommand)
parameter1 = EventArgsConverter == null
? parameter
: EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
if (command == null || !command.CanExecute(parameter1))
return;
command.Execute(parameter1);
}
private static void OnCommandChanged(EventToCommand element, DependencyPropertyChangedEventArgs e)
{
if (element == null)
return;
if (e.OldValue != null)
((ICommand) e.OldValue).CanExecuteChanged -= element.OnCommandCanExecuteChanged;
var newValue = (ICommand) e.NewValue;
if (newValue != null)
newValue.CanExecuteChanged += element.OnCommandCanExecuteChanged;
element.EnableDisableElement();
}
private bool AssociatedElementIsDisabled()
{
var associatedObject = GetAssociatedObject();
if (AssociatedObject == null)
return true;
if (associatedObject != null)
return !associatedObject.IsEnabled;
return false;
}
private void EnableDisableElement()
{
var associatedObject = GetAssociatedObject();
if (associatedObject == null)
return;
var command = GetCommand();
if (!MustToggleIsEnabledValue || command == null)
return;
associatedObject.IsEnabled = command.CanExecute(CommandParameterValue);
}
private void OnCommandCanExecuteChanged(object sender, EventArgs e) => EnableDisableElement();
}
}
public bool MustToggleIsEnabled
{
get => (bool) GetValue(MustToggleIsEnabledProperty);
set => SetValue(MustToggleIsEnabledProperty, ValueBoxes.BooleanBox(value));
}
public bool MustToggleIsEnabledValue
{
get
{
if (_mustToggleValue.HasValue)
return _mustToggleValue.Value;
return MustToggleIsEnabled;
}
set
{
_mustToggleValue = value;
EnableDisableElement();
}
}
public bool PassEventArgsToCommand { get; set; }
public IEventArgsConverter EventArgsConverter { get; set; }
public object EventArgsConverterParameter
{
get => GetValue(EventArgsConverterParameterProperty);
set => SetValue(EventArgsConverterParameterProperty, value);
}
public bool AlwaysInvokeCommand
{
get => (bool) GetValue(AlwaysInvokeCommandProperty);
set => SetValue(AlwaysInvokeCommandProperty, ValueBoxes.BooleanBox(value));
}
protected override void OnAttached()
{
base.OnAttached();
EnableDisableElement();
}
private FrameworkElement GetAssociatedObject() => AssociatedObject as FrameworkElement;
private ICommand GetCommand() => Command;
public void Invoke() => Invoke(null);
protected override void Invoke(object parameter)
{
if (AssociatedElementIsDisabled() && !AlwaysInvokeCommand)
return;
var command = GetCommand();
var parameter1 = CommandParameterValue;
if (parameter1 == null && PassEventArgsToCommand)
parameter1 = EventArgsConverter == null
? parameter
: EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
if (command == null || !command.CanExecute(parameter1))
return;
command.Execute(parameter1);
}
private static void OnCommandChanged(EventToCommand element, DependencyPropertyChangedEventArgs e)
{
if (element == null)
return;
if (e.OldValue != null)
((ICommand) e.OldValue).CanExecuteChanged -= element.OnCommandCanExecuteChanged;
var newValue = (ICommand) e.NewValue;
if (newValue != null)
newValue.CanExecuteChanged += element.OnCommandCanExecuteChanged;
element.EnableDisableElement();
}
private bool AssociatedElementIsDisabled()
{
var associatedObject = GetAssociatedObject();
if (AssociatedObject == null)
return true;
if (associatedObject != null)
return !associatedObject.IsEnabled;
return false;
}
private void EnableDisableElement()
{
var associatedObject = GetAssociatedObject();
if (associatedObject == null)
return;
var command = GetCommand();
if (!MustToggleIsEnabledValue || command == null)
return;
associatedObject.IsEnabled = command.CanExecute(CommandParameterValue);
}
private void OnCommandCanExecuteChanged(object sender, EventArgs e) => EnableDisableElement();
}