diff --git a/osrs-toolbox/MVVM/ModelBase.cs b/osrs-toolbox/MVVM/ModelBase.cs new file mode 100644 index 0000000..79a7ee2 --- /dev/null +++ b/osrs-toolbox/MVVM/ModelBase.cs @@ -0,0 +1,17 @@ +namespace osrs_toolbox +{ + public abstract class ModelBase : PropertyChangedBase + { + public void SetProperty(ref T storage, T value, string name) + { + if (storage == null) + throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nStorage cannot be null."); + if (value == null) + throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nValue cannot be null."); + if (storage.GetType() != value.GetType()) + throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nVariable type mismatch between storage and value."); + storage = value; + OnPropertyChanged(name); + } + } +} diff --git a/osrs-toolbox/MVVM/PropertyChangedBase.cs b/osrs-toolbox/MVVM/PropertyChangedBase.cs new file mode 100644 index 0000000..d5b272a --- /dev/null +++ b/osrs-toolbox/MVVM/PropertyChangedBase.cs @@ -0,0 +1,10 @@ +using System.ComponentModel; + +namespace osrs_toolbox +{ + public abstract class PropertyChangedBase : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + public void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + } +} diff --git a/osrs-toolbox/MVVM/RelayCommand.cs b/osrs-toolbox/MVVM/RelayCommand.cs new file mode 100644 index 0000000..524987d --- /dev/null +++ b/osrs-toolbox/MVVM/RelayCommand.cs @@ -0,0 +1,33 @@ +using System.Windows.Input; + +namespace osrs_toolbox +{ + public class RelayCommand : ICommand + { + private Action execute; + private Func? canExecute; + + + public event EventHandler? CanExecuteChanged + { + add { CommandManager.RequerySuggested += value; } + remove { CommandManager.RequerySuggested -= value; } + } + + public RelayCommand(Action execute, Func? canExecute = null) + { + this.execute = execute; + this.canExecute = canExecute; + } + + public bool CanExecute(object? parameter) + { + return this.canExecute == null || this.canExecute(parameter); + } + + public void Execute(object? parameter) + { + this.execute(parameter); + } + } +}