2021-12-18 17:40:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-11-28 13:18:21 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
2021-12-31 17:48:04 +00:00
|
|
|
|
namespace PettingZoo.WPF.ViewModel
|
2021-11-28 13:18:21 +00:00
|
|
|
|
{
|
|
|
|
|
public class BaseViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
2021-12-18 17:40:22 +00:00
|
|
|
|
private int commandsChangedDisabled;
|
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
|
|
|
|
|
protected virtual void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected bool SetField<T>(ref T field, T value, IEqualityComparer<T>? comparer = null, [CallerMemberName] string? propertyName = null,
|
2021-12-18 11:18:35 +00:00
|
|
|
|
DelegateCommand[]? delegateCommandsChanged = null,
|
|
|
|
|
string[]? otherPropertiesChanged = null)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
{
|
|
|
|
|
if ((comparer ?? EqualityComparer<T>.Default).Equals(field, value))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
field = value;
|
|
|
|
|
RaisePropertyChanged(propertyName);
|
|
|
|
|
|
2021-12-18 11:18:35 +00:00
|
|
|
|
if (otherPropertiesChanged != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var otherProperty in otherPropertiesChanged)
|
|
|
|
|
RaisePropertyChanged(otherProperty);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 17:40:22 +00:00
|
|
|
|
// ReSharper disable once InvertIf
|
2021-12-18 11:18:35 +00:00
|
|
|
|
if (delegateCommandsChanged != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var delegateCommand in delegateCommandsChanged)
|
|
|
|
|
delegateCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-12-18 17:40:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void DisableCommandsChanged(Action updateFields, params DelegateCommand[] delegateCommandsChangedAfter)
|
|
|
|
|
{
|
|
|
|
|
commandsChangedDisabled++;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
updateFields();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
commandsChangedDisabled--;
|
|
|
|
|
if (commandsChangedDisabled == 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var delegateCommand in delegateCommandsChangedAfter)
|
|
|
|
|
delegateCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-28 13:18:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|