main
syneffort 1 year ago
parent e3106bedde
commit 5eb21e41bd
  1. 2
      MVVMwithWPF/MVVMwithWPF/App.xaml
  2. 1
      MVVMwithWPF/MVVMwithWPF/Models/Person.cs
  3. 84
      MVVMwithWPF/MVVMwithWPF/ViewModels/CommandViewModel.cs
  4. 57
      MVVMwithWPF/MVVMwithWPF/Views/CommandWindow.xaml
  5. 27
      MVVMwithWPF/MVVMwithWPF/Views/CommandWindow.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/ConverterWindoew.xaml">
StartupUri="/Views/CommandWindow.xaml">
<Application.Resources />
</Application>

@ -13,5 +13,6 @@ namespace MVVMwithWPF.Models
public string Name { get; set; }
public bool Sex { get; set; }
public string DepartmentCode { get; set; }
public string Description { get; set; }
}
}

@ -0,0 +1,84 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MVVMwithWPF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace MVVMwithWPF.ViewModels
{
public partial class CommandViewModel : ObservableObject
{
[ObservableProperty]
private ObservableCollection<Person> _people;
[ObservableProperty]
private Person _selectedPerson;
[ObservableProperty]
private string _message;
[ObservableProperty]
private bool _isBusy;
public CommandViewModel()
{
People = new ObservableCollection<Person>()
{
new Person() { Id = 1, Name = "user001", Sex = true, DepartmentCode = "01"},
new Person() { Id = 2, Name = "user002", Sex = false, DepartmentCode = "02"},
new Person() { Id = 3, Name = "user003", Sex = true, DepartmentCode = "03"},
new Person() { Id = 4, Name = "user004", Sex = false, DepartmentCode = "02"},
new Person() { Id = 5, Name = "user005", Sex = true, DepartmentCode = "01"},
};
}
[RelayCommand]
private void Delete()
{
if (MessageBox.Show("선택한 아이템을 삭제하시겠습니까?", "확인", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
return;
People.Remove(SelectedPerson);
}
[RelayCommand]
private void AddNewItem(object obj)
{
if (obj is AddingNewItemEventArgs args)
args.NewItem = new Person() { Id = People.Max(p => p.Id) + 1 };
}
[RelayCommand]
private void SelectionChanged(Person obj)
{
Message = obj == null ? "선택한 아이템이 없습니다." : $"{obj.Name}을/를 선택했습니다.";
SelectedPerson = obj;
}
[RelayCommand]
private void Normal()
{
Message = "Normal 버튼을 클릭했습니다.";
}
[RelayCommand]
private async Task AsyncNormal()
{
Message = "비동기 Noraml 버튼 클릭 시작";
await Task.Delay(3000);
Message = "비동기 Noraml 버튼 클릭 종료";
}
}
}

@ -0,0 +1,57 @@
<Window x:Class="MVVMwithWPF.Views.CommandWindow"
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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMwithWPF.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:veiwModels="clr-namespace:MVVMwithWPF.ViewModels"
Title="CommandWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<veiwModels:CommandViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Margin="5">
<TextBlock Text="{Binding Message}"/>
<Button Command="{Binding NormalCommand}" Content="Normal"/>
<TextBlock Text="{Binding AsyncNormalCommand.IsRunning}"/>
<Button Command="{Binding AsyncNormalCommand}" Content="Async Normal"/>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<TextBlock Text="{Binding SelectedPerson.Name}"/>
<Button Width="70" Margin="10,0,0,0" Command="{Binding DeleteCommand}" Content="Delete"/>
</StackPanel>
<ListView x:Name="peopleListView" ItemsSource="{Binding People}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="SelectionChanged">
<b:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=peopleListView, Path=SelectedItem}"/>
</b:EventTrigger>
</b:Interaction.Triggers>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Id}" Header="Id"/>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
<GridViewColumn DisplayMemberBinding="{Binding Sex}" Header="Sex"/>
<GridViewColumn DisplayMemberBinding="{Binding Description}" Header="Description"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<DataGrid ItemsSource="{Binding People}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="AddingNewItem">
<b:InvokeCommandAction Command="{Binding AddNewItemCommand}" PassEventArgsToCommand="True"/>
</b:EventTrigger>
</b:Interaction.Triggers>
</DataGrid>
</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>
/// CommandWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class CommandWindow : Window
{
public CommandWindow()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save