Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetValue call to PropertyChangeBase #466

Closed
mrfichtn opened this issue Aug 12, 2017 · 1 comment
Closed

Add SetValue call to PropertyChangeBase #466

mrfichtn opened this issue Aug 12, 2017 · 1 comment

Comments

@mrfichtn
Copy link

When added to PropertyChangedBase, the following calls streamlines implementing properties:

    /// <summary>
    /// Sets property value, raising appropriate message
    /// </summary>
    /// <typeparam name="T">Property type</typeparam>
    /// <param name="oldValue">Property to set</param>
    /// <param name="newValue">New property value</param>
    /// <param name="propertyName">Name used when signaling event</param>
    protected bool SetValue<T>(ref T oldValue, T newValue, string propertyName)
    {
        bool changed;
        if (!Equals(oldValue, newValue))
        {
            oldValue = newValue;
            NotifyOfPropertyChange(propertyName);
            changed = true;
        }
        else
        {
            changed = false;
        }
        return changed;
    }

    /// <summary>
    /// Sets property value, refreshing all bindings
    /// </summary>
    /// <typeparam name="T">Property type</typeparam>
    /// <param name="oldValue">Property to set</param>
    /// <param name="newValue">New property value</param>
    protected bool SetValue<T>(ref T oldValue, T newValue)
    {
        bool changed;
        if (!Equals(oldValue, newValue))
        {
            oldValue = newValue;
            Refresh();
            changed = true;
        }
        else
        {
            changed = false;
        }
        return changed;

    }

    /// <summary>
    /// Sets value, raising changes messages for a group of properties
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="oldValue">Property to set</param>
    /// <param name="newValue">New property value</param>
    /// <param name="propertyNames">Properties affected by change in value</param>
    protected bool SetValue<T>(ref T oldValue, T newValue, params string[] propertyNames)
    {
        bool changed;
        if (!Equals(oldValue, newValue))
        {
            oldValue = newValue;
            foreach (var propertyName in propertyNames)
                NotifyOfPropertyChange(propertyName);
            changed = true;
        }
        else
        {
            changed = false;
        }
        return changed;
    }

For example:
#region Contents Property
public string Contents
{
get => _contents;
set => SetValue(ref _contents, value, nameof(Contents);
}
private string _contents;
#endregion

Thanks!

@nigel-sampson nigel-sampson added this to the v3.2.0 milestone Sep 3, 2017
@nigel-sampson
Copy link
Contributor

Considering adding something like to help reduce some boilerplate (though I recommend using something like PropertyChanged.Fody to get rid of backing fields altogether).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants