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 property changed callback with virtual instance method #144

Open
JohanLarsson opened this issue Jul 1, 2017 · 2 comments
Open

Add property changed callback with virtual instance method #144

JohanLarsson opened this issue Jul 1, 2017 · 2 comments

Comments

@JohanLarsson
Copy link
Collaborator

JohanLarsson commented Jul 1, 2017

Default hidden, for convenience.
Before:

namespace RoslynSandbox
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            nameof(Value),
            typeof(double),
            typeof(FooControl),
            new PropertyMetadata(default(double)));

        public double Value
        {
            get => (double)this.GetValue(ValueProperty);
            set => this.SetValue(ValueProperty, value);
        }
    }
}

After:

namespace RoslynSandbox
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            nameof(Value),
            typeof(double),
            typeof(FooControl),
            new PropertyMetadata(
                default(double),
                (d, e) => ((FooControl)d).OnValueChanged((double)e.OldValue, (double)e.NewValue)));

        public double Value
        {
            get => (double)this.GetValue(ValueProperty);
            set => this.SetValue(ValueProperty, value);
        }

        /// <summary>
        /// This method is invoked when <see cref="ValueProperty"/> changes value.
        /// </summary>
        /// <param name="oldValue">The old value of <see cref="ValueProperty"/>.</param>
        /// <param name="newValue">The new value of <see cref="ValueProperty"/>.</param>
        protected virtual void OnValueChanged(double oldValue, double newValue)
        {
        }
    }
}
@JohanLarsson
Copy link
Collaborator Author

Maybe better as a refactoring.

@JohanLarsson JohanLarsson changed the title New Analyzer: generate property changed callback with virtual instance method New Analyzer + fix: property changed callback with virtual instance method Jul 30, 2018
@JohanLarsson
Copy link
Collaborator Author

Refactorings via nuget will work in some future VS version.

@JohanLarsson JohanLarsson changed the title New Analyzer + fix: property changed callback with virtual instance method Add property changed callback with virtual instance method Oct 13, 2020
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

1 participant