1
0
mirror of synced 2024-11-15 01:33:51 +00:00
PettingZoo/PettingZoo.WPF/ViewModel/BaseViewModel.cs
Mark van Renswoude 133adf205c WIP: generate examples from Tapeti message classes from NuGet feeds
- Implementation of assembly/nuget package selection UI
- Actual example generation needs more work
2021-12-31 18:48:04 +01:00

65 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace PettingZoo.WPF.ViewModel
{
public class BaseViewModel : INotifyPropertyChanged
{
private int commandsChangedDisabled;
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,
DelegateCommand[]? delegateCommandsChanged = null,
string[]? otherPropertiesChanged = null)
{
if ((comparer ?? EqualityComparer<T>.Default).Equals(field, value))
return false;
field = value;
RaisePropertyChanged(propertyName);
if (otherPropertiesChanged != null)
{
foreach (var otherProperty in otherPropertiesChanged)
RaisePropertyChanged(otherProperty);
}
// ReSharper disable once InvertIf
if (delegateCommandsChanged != null)
{
foreach (var delegateCommand in delegateCommandsChanged)
delegateCommand.RaiseCanExecuteChanged();
}
return true;
}
protected void DisableCommandsChanged(Action updateFields, params DelegateCommand[] delegateCommandsChangedAfter)
{
commandsChangedDisabled++;
try
{
updateFields();
}
finally
{
commandsChangedDisabled--;
if (commandsChangedDisabled == 0)
{
foreach (var delegateCommand in delegateCommandsChangedAfter)
delegateCommand.RaiseCanExecuteChanged();
}
}
}
}
}