์ ๊ธฐํ๊ฒ๋ WinUI์ ToggleSwitch ์ปจํธ๋กค์ Command
์์ฑ์ด ์์ต๋๋ค. Command์ ์ฅ์ ์ค ํ๋๊ฐ ์คํํ ์ ์๋ ์ผ์ด์ค์ ๊ฒฝ์ฐ ๋นํ์ฑํ ํ๋ ๊ธฐ๋ฅ์ธ๋ฐ์, ์ด ๊ธฐ๋ฅ ๋๋ฌธ์ด๋ผ๋ Command
๊ฐ ํ์ํฉ๋๋ค.
์ํ์ฐฉ์ค๊ฐ ์์์ต๋๋ค.
Command
์CanExecuteChanged
์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ToggleSwitch
์ปจํธ๋กค์ ์ธ์คํด์ค๋ฅผ ์ฐพ์์ผ ํ๋๋ฐAttached Property
๋ก๋ ์ ์ ์์ผ๋ฏ๋กBehavior
๋ก ๋ค์ ๊ตฌํํ์์ต๋๋ค. ๊ธ ์ค๋ฅธ์ชฝ ์๋จ์ ์ฐํ ๋ชจ์์ ํตํด ์ํ์ฐฉ์ค๋ฅผ ํ์ธํ์ธ์!
Command
์ CommandParameter
๊ธฐ๋ฅ์ ํ์ฅํ๋ Behavior
๋ฅผ ๋ค์์ฒ๋ผ ์ฌ์ฉํ ์ ์๊ฒ ๋ฉ๋๋ค.
| XAML
<ToggleSwitch IsOn="{x:Bind SelectedAttribute.IsKey, Mode=OneWay}">
<interactivity:Interaction.Behaviors>
<extensions:ToggleSwitchCommandBehavior Command="{x:Bind TogglePKCommand}" />
</interactivity:Interaction.Behaviors>
</ToggleSwitch>
| ToggleSwitchCommandBehavior.cs
public class ToggleSwitchCommandBehavior : Behavior<ToggleSwitch>
{
public readonly static DependencyProperty CommandProperty =
DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(ToggleSwitchCommandBehavior),
new PropertyMetadata(null, OnIsExternalChanged));
public readonly static DependencyProperty CommandParameterProperty =
DependencyProperty.Register(nameof(CommandParameter), typeof(object), typeof(ToggleSwitchCommandBehavior),
new PropertyMetadata(null));
private bool _isAccessUserInputProperty;
public ICommand? Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public object? CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
protected override void OnAttached()
{
AssociatedObject.PointerReleased += ToggleSwitch_PointerReleased;
AssociatedObject.PreviewKeyDown += ToggleSwitch_PreviewKeyDown;
AssociatedObject.Toggled += ToggleSwitch_Toggled;
if (Command is not null)
{
Command_CanExecuteChanged(this, EventArgs.Empty);
}
}
protected override void OnDetaching()
{
AssociatedObject.PointerReleased -= ToggleSwitch_PointerReleased;
AssociatedObject.PreviewKeyDown -= ToggleSwitch_PreviewKeyDown;
AssociatedObject.Toggled -= ToggleSwitch_Toggled;
}
private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
{
if (sender is not ToggleSwitchCommandBehavior @this)
return;
if (args.OldValue is ICommand oldCommand)
oldCommand.CanExecuteChanged -= @this.Command_CanExecuteChanged;
if (args.NewValue is ICommand newCommand)
{
newCommand.CanExecuteChanged += @this.Command_CanExecuteChanged;
if (@this.AssociatedObject is not null)
@this.Command_CanExecuteChanged(@this, EventArgs.Empty);
}
}
private void Command_CanExecuteChanged(object? sender, EventArgs e)
{
if (Command is null)
return;
AssociatedObject.IsEnabled = Command.CanExecute(CommandParameter);
}
private void ToggleSwitch_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key is VirtualKey.Space)
{
_isAccessUserInputProperty = true;
}
}
private void ToggleSwitch_PointerReleased(object sender, PointerRoutedEventArgs e)
{
_isAccessUserInputProperty = true;
}
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
var toggleSwitch = (ToggleSwitch)sender;
// ์ฌ์ฉ์ ์
๋ ฅ์ผ ๋๋ง ์ฒ๋ฆฌํ๋๋ก ํ๋ค.
if (_isAccessUserInputProperty is false)
return;
_isAccessUserInputProperty = false;
if (Command is null)
return;
// ์ปค๋ฉ๋์ ์ํด ๊ฐ์ด ๋ณํ๋๋ก IsOn ๊ฐ์ ์๋ณตํ๋ค.
toggleSwitch.IsOn = !toggleSwitch.IsOn;
if (Command.CanExecute(CommandParameter))
Command.Execute(CommandParameter);
}
}