Dreamine.MVVM.Behaviors 1.0.4
Dreamine.MVVM.Behaviors 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
EnterKeyCommandBehavior.cs
이 파일의 문서화 페이지로 가기
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Input;
4using Dreamine.MVVM.Behaviors.Core.Base;
5
7{
16 public class EnterKeyCommandBehavior : Behavior<UIElement>
17 {
26 public static readonly DependencyProperty CommandProperty =
27 DependencyProperty.RegisterAttached(
28 "Command",
29 typeof(ICommand),
31 new PropertyMetadata(null, OnCommandChanged));
32
57 public static ICommand? GetCommand(DependencyObject obj)
58 {
59 return (ICommand?)obj.GetValue(CommandProperty);
60 }
61
86 public static void SetCommand(DependencyObject obj, ICommand? value)
87 {
88 obj.SetValue(CommandProperty, value);
89 }
90
115 private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
116 {
117 if (d is UIElement element)
118 {
119 element.KeyDown -= OnKeyDown;
120 element.KeyDown += OnKeyDown;
121 }
122 }
123
148 private static void OnKeyDown(object sender, KeyEventArgs e)
149 {
150 if (e.Key == Key.Enter && sender is DependencyObject obj)
151 {
152 ICommand? command = GetCommand(obj);
153 if (command?.CanExecute(null) == true)
154 {
155 command.Execute(null);
156 e.Handled = true;
157 }
158 }
159 }
160 }
161}
static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static void OnKeyDown(object sender, KeyEventArgs e)
static void SetCommand(DependencyObject obj, ICommand? value)