We have implemented a Undo/Redo-Manager based on your BindableBase class and your PropertyManager.
We used this to detect changes in the ViewModel and record them for Undo/Redo purposes.
Our BindableBase derived base class for all ViewModels looked like this:
C#public class ChangeTrackingVm : BindableBase
{
private UndoPropertyManager _propertyManager;
protected ChangeTrackingVm()
{
}
protected UndoPropertyManager PropertyManager
{
get { return _propertyManager ?? (_propertyManager = new UndoPropertyManager(this)); }
}
protected override PropertyManager CreatePropertyManager()
{
return PropertyManager;
}
protected void InitProperty<T>(Expression<Func<T>> expression, T initValue)
{
SetPropertyCore(GetPropertyName(expression), initValue);
}
/// <summary>
/// Sets property value without Undo-Tracking
/// </summary>
protected internal void SetPropertyCore<T>(string propertyName, T value)
{
PropertyManager.SetPropertyWithoutTracking(propertyName, value);
}
protected void AddPropertyAction<T>(string propertyName, T oldValue, T newValue)
{
if (UndoService.Service != null &&
GetType().GetProperty(propertyName)?.GetAttribute<NoUndoTrackingAttribute>() == null)
{
UndoService.Service?.AddPropertyAction(this, propertyName, oldValue, newValue);
}
}
protected class UndoPropertyManager : PropertyManager
{
private readonly ChangeTrackingVm _viewModel;
public UndoPropertyManager(ChangeTrackingVm viewModel)
{
_viewModel = viewModel;
}
protected override bool SetPropertyCore<T>(string propertyName, T value, out T oldValue)
{
bool result = base.SetPropertyCore(propertyName, value, out oldValue);
_viewModel.AddPropertyAction(propertyName, oldValue, value);
return result;
}
internal void SetPropertyWithoutTracking<T>(string propertyName, T value)
{
base.SetPropertyCore(propertyName, value, out _);
}
}
}
Now you removed the PropertyManager and we are thinking about how to replace the lost functionality.
Your new BindableBase offers no virtual methods where we could dock our change detection logic.
Can you give us an idea how to solve this?
Ulrich