Files
laatzen/Common/TestUI/ViewModels/BaseVM.cs
T

86 lines
2.3 KiB
C#

namespace TestUI.ViewModels
{
using System;
using System.ComponentModel;
using System.Windows.Input;
public abstract class BaseVM : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
protected BaseVM()
{
App.Close += this.Dispose;
}
protected void NotifyPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
protected void Set<T>(ref T oldValue, T newValue, string property)
{
oldValue = newValue;
this.NotifyPropertyChanged(property);
}
public void Dispose()
{
App.Close -= this.Dispose;
}
internal class Command : ICommand
{
private readonly Action commandHandler;
public Command(Action commandHandler)
{
this.commandHandler = commandHandler;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object parameter)
{
return this.commandHandler != null;
}
public void Execute(object parameter)
{
this.commandHandler?.Invoke();
}
}
internal class Command<T> : ICommand
{
private readonly Action<T> commandCallback;
public Command(Action<T> commandCallback)
{
this.commandCallback = commandCallback;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object parameter)
{
return this.commandCallback != null;
}
public void Execute(object parameter)
{
this.commandCallback?.Invoke(parameter is T value ? value : default(T));
}
}
}
}