using AddressBook_MVVMSample.Command; using AddressBook_MVVMSample.Model; using AddressBook_MVVMSample.SubWindow; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; 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.Navigation; using System.Windows.Shapes; namespace AddressBook_MVVMSample { /// /// MainWindow.xaml에 대한 상호 작용 논리 /// public partial class MainWindow : Window, INotifyPropertyChanged { public DelegateCommand AddCommand { get; set; } public DelegateCommand DeleteCommand { get; set; } public DelegateCommand ModiftyCommand { get; set; } public DelegateCommand ExitCommand { get; set; } public ObservableCollection Persons { get; set; } public event PropertyChangedEventHandler PropertyChanged; public MainWindow() { InitializeComponent(); InitInstance(); } private void InitInstance() { this.DataContext = this; this.Persons = new ObservableCollection(); this.Persons.Add(new Person() { Name = "James", Gender = true, PhoneNumber = "123-456-7890", Address = "NewYork" }); this.Persons.Add(new Person() { Name = "Thomas", Gender = true, PhoneNumber = "234-456-7890", Address = "Callifornia" }); this.Persons.Add(new Person() { Name = "Richard", Gender = true, PhoneNumber = "345-456-7890", Address = "Seoul" }); this.Persons.Add(new Person() { Name = "Sally", Gender = false, PhoneNumber = "456-456-7890", Address = "Beijing" }); this.AddCommand = new DelegateCommand(CommandAddAction); this.DeleteCommand = new DelegateCommand(CommandDeleteAction); this.ModiftyCommand = new DelegateCommand(CommandModifyAction); this.ExitCommand = new DelegateCommand(CommandExitAction); } private int _selectedIndex; public int SelectedIndex { get { return _selectedIndex; } set { _selectedIndex = value; OnPropertyChanged(nameof(SelectedIndex)); } } private void CommandAddAction(object obj) { AddViewWindow add = new AddViewWindow(); if (add.ShowDialog() != true) return; this.Persons.Add(add.PersonData); } private void CommandDeleteAction(object obj) { if (this.SelectedIndex < 0) { MessageBox.Show("No selected data.", "Address Book v0.1", MessageBoxButton.OK, MessageBoxImage.Information); return; } this.Persons.RemoveAt(this.SelectedIndex); } private void CommandModifyAction(object obj) { if (this.SelectedIndex < 0) { MessageBox.Show("No selected data.", "Address Book v0.1", MessageBoxButton.OK, MessageBoxImage.Information); return; } Person selectedData = this.Persons[this.SelectedIndex]; Person modifiedData = new Person(selectedData); AddViewWindow modify = new AddViewWindow(modifiedData, AddViewWindow.ViewType.Modify); if (modify.ShowDialog() != true) return; //this.Persons[this.SelectedIndex] = modifiedData; selectedData = modifiedData; } private void CommandExitAction(object obj) { this.Close(); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }