From 1286066abc1db009f33cc1f0a8e1dc7aefc4aadb Mon Sep 17 00:00:00 2001 From: Tracy Pearson Date: Thu, 11 Aug 2022 22:50:48 -0400 Subject: [PATCH] Basic setup completed --- WpfViewModelFirst/App.xaml | 3 +- WpfViewModelFirst/App.xaml.cs | 6 +++ WpfViewModelFirst/CustomCommand.cs | 49 ++++++++++++++++++++++++ WpfViewModelFirst/MainWindowViewModel.cs | 13 +++++++ WpfViewModelFirst/ObservableObject.cs | 31 +++++++++++++++ WpfViewModelFirst/ViewModelBase.cs | 13 +++++++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 WpfViewModelFirst/CustomCommand.cs create mode 100644 WpfViewModelFirst/MainWindowViewModel.cs create mode 100644 WpfViewModelFirst/ObservableObject.cs create mode 100644 WpfViewModelFirst/ViewModelBase.cs diff --git a/WpfViewModelFirst/App.xaml b/WpfViewModelFirst/App.xaml index 16cc4cd..511a951 100644 --- a/WpfViewModelFirst/App.xaml +++ b/WpfViewModelFirst/App.xaml @@ -1,8 +1,7 @@  + xmlns:local="clr-namespace:WpfViewModelFirst"> diff --git a/WpfViewModelFirst/App.xaml.cs b/WpfViewModelFirst/App.xaml.cs index 3a8b1c9..63bec40 100644 --- a/WpfViewModelFirst/App.xaml.cs +++ b/WpfViewModelFirst/App.xaml.cs @@ -13,5 +13,11 @@ namespace WpfViewModelFirst /// public partial class App : Application { + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + MainWindow mainWindow = new() { DataContext = new MainWindowViewModel() }; + mainWindow.Show(); + } } } diff --git a/WpfViewModelFirst/CustomCommand.cs b/WpfViewModelFirst/CustomCommand.cs new file mode 100644 index 0000000..05643ac --- /dev/null +++ b/WpfViewModelFirst/CustomCommand.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace WpfViewModelFirst +{ + public class CustomCommand : ICommand + { + private readonly Action _execute; + private readonly Predicate? _canExecute; + + public CustomCommand(Action execute, Predicate canExecute) + { + _execute = execute; + _canExecute = canExecute; + } + + public CustomCommand(Action execute) + { + _execute = execute; + } + + public bool CanExecute(object? parameter) + { + return _canExecute == null || _canExecute(parameter); + } + + public event EventHandler? CanExecuteChanged + { + add + { + CommandManager.RequerySuggested += value; + } + remove + { + CommandManager.RequerySuggested -= value; + } + } + + public void Execute(object? parameter) + { + _execute(parameter); + } + + } +} diff --git a/WpfViewModelFirst/MainWindowViewModel.cs b/WpfViewModelFirst/MainWindowViewModel.cs new file mode 100644 index 0000000..e15da55 --- /dev/null +++ b/WpfViewModelFirst/MainWindowViewModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WpfViewModelFirst +{ + public class MainWindowViewModel : ViewModelBase + { + + } +} diff --git a/WpfViewModelFirst/ObservableObject.cs b/WpfViewModelFirst/ObservableObject.cs new file mode 100644 index 0000000..9fc48f9 --- /dev/null +++ b/WpfViewModelFirst/ObservableObject.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace WpfViewModelFirst +{ + public class ObservableObject + { + public event PropertyChangedEventHandler? PropertyChanged; + protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = "") + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + protected virtual bool Set(ref T storage, T value, [CallerMemberName] string propertyName = "") + { + if (EqualityComparer.Default.Equals(storage, value)) + { + return false; + } + storage = value; + this.RaisePropertyChanged(propertyName); + return true; + } + + } +} diff --git a/WpfViewModelFirst/ViewModelBase.cs b/WpfViewModelFirst/ViewModelBase.cs new file mode 100644 index 0000000..fd3023c --- /dev/null +++ b/WpfViewModelFirst/ViewModelBase.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WpfViewModelFirst +{ + public class ViewModelBase : ObservableObject + { + + } +}