laatzen/Common/Ui/Common.UI.VFM/ViewModels/AppViewModel.cs
2024-07-01 08:42:23 +02:00

89 lines
2.6 KiB
C#

namespace Common.UI.VFM.ViewModels
{
using Common.UI.VFM.Views;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
public class AppViewModel : INotifyPropertyChanged
{
protected AppViewModel() => App.Exiting += this.Dispose;
public event PropertyChangedEventHandler PropertyChanged;
protected String PortName => ConfigurationManager.AppSettings[nameof(this.PortName)];
protected String SqlConnectionString => ConfigurationManager.AppSettings[nameof(this.SqlConnectionString)];
protected String LabelServer => ConfigurationManager.AppSettings[nameof(this.LabelServer)];
public virtual void Dispose() => App.Exiting -= this.Dispose;
protected void NotifyPropertyChanged([CallerMemberName] String property = "")
=> this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
protected void Set(Action setExpression, [CallerMemberName] String property = "")
{
setExpression?.Invoke();
this.NotifyPropertyChanged(property);
}
protected class Command : ICommand
{
private readonly Action executable;
public Command(Action executable)
{
this.executable = executable;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(Object _)
{
return this.executable != null;
}
public void Execute(Object _)
{
this.executable?.Invoke();
}
}
protected class Command<T> : ICommand
{
private readonly Action<T> executable;
public Command(Action<T> executable)
{
this.executable = executable;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(Object _)
{
return this.executable != null;
}
public void Execute(Object parameter)
{
this.executable?.Invoke(parameter is T t ? t : default(T));
}
}
}
}