diff --git a/MVVMwithWPF/MVVMwithWPF/App.xaml b/MVVMwithWPF/MVVMwithWPF/App.xaml index f6cd562..dc3752f 100644 --- a/MVVMwithWPF/MVVMwithWPF/App.xaml +++ b/MVVMwithWPF/MVVMwithWPF/App.xaml @@ -2,6 +2,6 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVMwithWPF" - StartupUri="/Views/CommandWindow.xaml"> + StartupUri="/Views/BehaviorWindow.xaml"> diff --git a/MVVMwithWPF/MVVMwithWPF/Behaviors/TextBoxBehavior.cs b/MVVMwithWPF/MVVMwithWPF/Behaviors/TextBoxBehavior.cs new file mode 100644 index 0000000..c9f9136 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Behaviors/TextBoxBehavior.cs @@ -0,0 +1,58 @@ +using Microsoft.Xaml.Behaviors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace MVVMwithWPF.Behaviors +{ + internal class TextBoxBehavior : Behavior + { + public bool IsSelectAll { get; set; } = true; + + protected override void OnAttached() + { + if (IsSelectAll) + AssociatedObject.GotFocus += AssociatedObject_GotFocus; + + AssociatedObject.KeyDown += AssociatedObject_KeyDown; + } + + private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) + { + if (e.Key == Key.Enter && EnterCommand != null) + { + EnterCommand.Execute(e); + e.Handled = true; + } + } + + private void AssociatedObject_GotFocus(object sender, System.Windows.RoutedEventArgs e) + { + Dispatcher.BeginInvoke(() => + { + AssociatedObject.SelectAll(); + }, null); + } + + protected override void OnDetaching() + { + if (IsSelectAll) + AssociatedObject.GotFocus -= AssociatedObject_GotFocus; + + AssociatedObject.KeyDown -= AssociatedObject_KeyDown; + } + + public ICommand EnterCommand + { + get { return (ICommand)GetValue(EnterCommandProperty); } + set { SetValue(EnterCommandProperty, value); } + } + + public static readonly DependencyProperty EnterCommandProperty = DependencyProperty.Register(nameof(EnterCommand), typeof(ICommand), typeof(TextBoxBehavior), new PropertyMetadata(null)); + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/ViewModels/BehaviorViewModel.cs b/MVVMwithWPF/MVVMwithWPF/ViewModels/BehaviorViewModel.cs new file mode 100644 index 0000000..286d8a7 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/ViewModels/BehaviorViewModel.cs @@ -0,0 +1,45 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace MVVMwithWPF.ViewModels +{ + public partial class BehaviorViewModel : ObservableObject + { + + [ObservableProperty] + private string _name; + + [ObservableProperty] + private string _phone; + + [ObservableProperty] + private string _email; + + [ObservableProperty] + private string _address; + + [ObservableProperty] + private string _company; + + [ObservableProperty] + private string _description; + + [RelayCommand] + private void Enter() + { + MessageBox.Show("Enter키를 입력했습니다."); + } + + [RelayCommand] + private void Send() + { + MessageBox.Show($"Name: {Name}{Environment.NewLine}Phone: {Phone}{Environment.NewLine}Email: {Email}{Environment.NewLine}", "알림"); + } + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/Views/BehaviorWindow.xaml b/MVVMwithWPF/MVVMwithWPF/Views/BehaviorWindow.xaml new file mode 100644 index 0000000..03cf601 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Views/BehaviorWindow.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +