I've not been able to find any examples at all for this.
Here is what I am currently doing:
public partial class RenameGroupPopupViewModel
{
private string _newGroupName;
public string NewGroupName
{
get => _newGroupName;
set => SetProperty(ref _newGroupName, value);
}
}
In ObservableObject.cs there is a parameter validateValue but I do not know or understand how to use it.
I would like to try using the Func<T, T, bool>? validateValue = null) to call a method called ValidateName that would check if the value is set to "default" and if so then return false so the value would not be set.
Here is the full code for the class:
Here is the code snippet from
/// <summary>
/// Sets the property.
/// </summary>
/// <returns><c>true</c>, if property was set, <c>false</c> otherwise.</returns>
/// <param name="backingStore">Backing store.</param>
/// <param name="value">Value.</param>
/// <param name="validateValue">Validates value.</param>
/// <param name="propertyName">Property name.</param>
/// <param name="onChanged">On changed.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
protected virtual bool SetProperty<T>(
ref T backingStore,
T value,
[CallerMemberName] string propertyName = "",
Action? onChanging = null,
Action? onChanged = null,
Func<T, T, bool>? validateValue = null)
{
// if value didn't change
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
// if value changed but didn't validate
if (validateValue != null && !validateValue(backingStore, value))
return false;
onChanging?.Invoke();
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
I realize I have to supply a function for the validation but I am not sure how to specify this and should it be a normal kind of method or a delegate? Here is the functionality I need:
private bool ValidateName(string groupName)
{
if (groupName == "default")
return false;
else
return true;
}