main
syneffort 1 year ago
parent 5eb21e41bd
commit c2e15402d1
  1. 2
      MVVMwithWPF/MVVMwithWPF/App.xaml
  2. 58
      MVVMwithWPF/MVVMwithWPF/Behaviors/TextBoxBehavior.cs
  3. 45
      MVVMwithWPF/MVVMwithWPF/ViewModels/BehaviorViewModel.cs
  4. 45
      MVVMwithWPF/MVVMwithWPF/Views/BehaviorWindow.xaml
  5. 27
      MVVMwithWPF/MVVMwithWPF/Views/BehaviorWindow.xaml.cs
  6. 203
      MVVMwithWPF/MVVMwithWPF/Views/SliderCheckBoxWindow.xaml
  7. 27
      MVVMwithWPF/MVVMwithWPF/Views/SliderCheckBoxWindow.xaml.cs

@ -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">
<Application.Resources />
</Application>

@ -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<TextBox>
{
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));
}
}

@ -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}", "알림");
}
}
}

@ -0,0 +1,45 @@
<Window x:Class="MVVMwithWPF.Views.BehaviorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:MVVMwithWPF.Behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMwithWPF.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MVVMwithWPF.ViewModels"
Title="BehaviorWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<viewModels:BehaviorViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Margin="5">
<TextBlock Text="Please enter your user information" />
<TextBlock Text="Name" />
<TextBox Text="{Binding Name}">
<b:Interaction.Behaviors>
<behaviors:TextBoxBehavior EnterCommand="{Binding EnterCommand}" />
</b:Interaction.Behaviors>
</TextBox>
<TextBlock Text="Phone" />
<TextBox Text="{Binding Phone}">
<b:Interaction.Behaviors>
<behaviors:TextBoxBehavior />
</b:Interaction.Behaviors>
</TextBox>
<TextBlock Text="Email" />
<TextBox Text="{Binding Email}">
<b:Interaction.Behaviors>
<behaviors:TextBoxBehavior EnterCommand="{Binding EnterCommand}" />
</b:Interaction.Behaviors>
</TextBox>
<Button Margin="0,10,0,0" Content="Send" Command="{Binding SendCommand}"/>
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,27 @@
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVMwithWPF.Views
{
/// <summary>
/// BehaviorWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class BehaviorWindow : Window
{
public BehaviorWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,203 @@
<Window x:Class="MVVMwithWPF.Views.SliderCheckBoxWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMwithWPF.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="SliderCheckBoxWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.Resources>
<!-- 색상 -->
<Color x:Key="GreenColor">#00C28C</Color>
<Color x:Key="LightGrayColor">#C5C5C5</Color>
<SolidColorBrush x:Key="GreenBrush" Color="{StaticResource GreenColor}" />
<SolidColorBrush x:Key="LightGrayBrush" Color="{StaticResource LightGrayColor}" />
<!-- 스타일 -->
<Style x:Key="SliderCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="MaxHeight" Value="20" />
<Setter Property="MaxWidth" Value="35" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="StoryboardIsChecked">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="14" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="StoryboardIsCheckedOff">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="14" />
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<BulletDecorator Background="Transparent" SnapsToDevicePixels="true">
<BulletDecorator.Bullet>
<Border x:Name="ForegroundPanel"
Width="35" Height="20"
BorderThickness="1" CornerRadius="10">
<Canvas>
<!-- 원 색상 -->
<Border x:Name="CheckFlag"
Width="19" Height="18"
VerticalAlignment="Center"
Background="White" BorderThickness="1" CornerRadius="10" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Border.RenderTransform>
<Border.Effect>
<DropShadowEffect Direction="180" ShadowDepth="1" />
</Border.Effect>
</Border>
</Canvas>
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="Center"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" />
<Setter Property="Padding" Value="4,0,0,0" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="ForegroundPanel" Property="Background" Value="{StaticResource GreenBrush}" />
<Trigger.EnterActions>
<BeginStoryboard x:Name="BeginStoryboardCheckedTrue" Storyboard="{StaticResource StoryboardIsChecked}" />
<RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedFalse" />
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="ForegroundPanel" Property="Background" Value="{StaticResource LightGrayBrush}" />
<Trigger.EnterActions>
<BeginStoryboard x:Name="BeginStoryboardCheckedFalse" Storyboard="{StaticResource StoryboardIsCheckedOff}" />
<RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedTrue" />
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SliderCheckBox_Square" TargetType="{x:Type CheckBox}">
<Setter Property="MaxHeight" Value="20" />
<Setter Property="MaxWidth" Value="35" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="StoryboardIsChecked">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="14" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="StoryboardIsCheckedOff">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="14" />
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<BulletDecorator Background="Transparent" SnapsToDevicePixels="true">
<BulletDecorator.Bullet>
<Border x:Name="ForegroundPanel"
Width="35" Height="20"
BorderThickness="1" CornerRadius="3">
<Canvas>
<!-- 원 색상 -->
<Border x:Name="CheckFlag"
Width="19" Height="18"
VerticalAlignment="Center"
Background="White" BorderThickness="1" CornerRadius="3" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Border.RenderTransform>
<Border.Effect>
<DropShadowEffect Direction="180" ShadowDepth="1" />
</Border.Effect>
</Border>
</Canvas>
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="Center"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" />
<Setter Property="Padding" Value="4,0,0,0" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="ForegroundPanel" Property="Background" Value="{StaticResource GreenBrush}" />
<Trigger.EnterActions>
<BeginStoryboard x:Name="BeginStoryboardCheckedTrue" Storyboard="{StaticResource StoryboardIsChecked}" />
<RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedFalse" />
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="ForegroundPanel" Property="Background" Value="{StaticResource LightGrayBrush}" />
<Trigger.EnterActions>
<BeginStoryboard x:Name="BeginStoryboardCheckedFalse" Storyboard="{StaticResource StoryboardIsCheckedOff}" />
<RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedTrue" />
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="CheckRadioFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="14,0,0,0"
SnapsToDevicePixels="true"
Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
StrokeDashArray="1 2" StrokeThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<CheckBox Style="{StaticResource SliderCheckBox}"/>
<CheckBox Style="{StaticResource SliderCheckBox_Square}"/>
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,27 @@
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVMwithWPF.Views
{
/// <summary>
/// SliderCheckBoxWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class SliderCheckBoxWindow : Window
{
public SliderCheckBoxWindow()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save