57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Windows.Input;
|
|
|
|
namespace Tasklight.Controller
|
|
{
|
|
public abstract class ModelBase : PropertyChangedBase
|
|
{
|
|
public void SetProperty<T>(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("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);
|
|
}
|
|
}
|
|
|
|
public abstract class PropertyChangedBase : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
public void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
|
}
|
|
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private Action<object> execute;
|
|
private Func<object, bool>? canExecute;
|
|
|
|
|
|
public event EventHandler? CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
public RelayCommand(Action<object> execute, Func<object, bool>? 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);
|
|
}
|
|
}
|
|
}
|