Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:05:26+00:00 2026-05-24T13:05:26+00:00

We have a WPF Project that follows the MVVM pattern. In the View Model

  • 0

We have a WPF Project that follows the MVVM pattern.

In the View Model there is a lot of code that looks like this:

    private string m_Fieldname;
    public string Fieldname
    {
        get { return m_Fieldname; }
        set
        {
            m_Fieldname = value;
            OnPropertyChanged("Fieldname");
        }
    }

Is there a way to do this that would require less code?

Would be nice with something like this:

[NotifyWhenChanged]
public string Fieldname { get; set ; }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:05:27+00:00Added an answer on May 24, 2026 at 1:05 pm

    You could have a look at PostSharp. They even have a sample at Data Binding. The code taken from there:

    /// <summary>
    /// Aspect that, when apply on a class, fully implements the interface 
    /// <see cref="INotifyPropertyChanged"/> into that class, and overrides all properties to
    /// that they raise the event <see cref="INotifyPropertyChanged.PropertyChanged"/>.
    /// </summary>
    [Serializable]
    [IntroduceInterface( typeof(INotifyPropertyChanged), 
                         OverrideAction = InterfaceOverrideAction.Ignore )]
    [MulticastAttributeUsage( MulticastTargets.Class, 
                              Inheritance = MulticastInheritance.Strict )]
    public sealed class NotifyPropertyChangedAttribute : InstanceLevelAspect, 
                                                         INotifyPropertyChanged
    {
    
        /// <summary>
        /// Field bound at runtime to a delegate of the method <c>OnPropertyChanged</c>.
        /// </summary>
        [ImportMember( "OnPropertyChanged", IsRequired = false)] 
        public Action<string> OnPropertyChangedMethod;
    
        /// <summary>
        /// Method introduced in the target type (unless it is already present);
        /// raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        [IntroduceMember( Visibility = Visibility.Family, IsVirtual = true, 
                          OverrideAction = MemberOverrideAction.Ignore )]
        public void OnPropertyChanged( string propertyName )
        {
            if ( this.PropertyChanged != null )
            {
               this.PropertyChanged( this.Instance, 
                                      new PropertyChangedEventArgs( propertyName ) );
            }
        }
    
        /// <summary>
        /// Event introduced in the target type (unless it is already present);
        /// raised whenever a property has changed.
        /// </summary>
        [IntroduceMember( OverrideAction = MemberOverrideAction.Ignore )]
        public event PropertyChangedEventHandler PropertyChanged;
    
        /// <summary>
        /// Method intercepting any call to a property setter.
        /// </summary>
        /// <param name="args">Aspect arguments.</param>
        [OnLocationSetValueAdvice, 
         MulticastPointcut( Targets = MulticastTargets.Property, 
             Attributes = MulticastAttributes.Instance)]
        public void OnPropertySet( LocationInterceptionArgs args )
        {
            // Don't go further if the new value is equal to the old one.
            // (Possibly use object.Equals here).
            if ( args.Value == args.GetCurrentValue() ) return;
    
            // Actually sets the value.
            args.ProceedSetValue();
    
            // Invoke method OnPropertyChanged (our, the base one, or the overridden one).
            this.OnPropertyChangedMethod.Invoke( args.Location.Name );
    
        }
    }
    

    Usage is then as simple as this:

    [NotifyPropertyChanged]
    public class Shape
    {
       public double X { get; set; }
       public double Y { get; set; }
    }
    

    Examples taken from PostSharp site and inserted for completing the answer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing with my first wpf project using the mvvm pattern. I have Microsoft
I have a WPF project, and there is one dialog that I will re-use
I have a WPF project like this: In a xaml file in my RCISP.WPF
I have a project I'm working on that requires our WPF application read SMS
I have a WPF project which compiles just fine. However, when I enable code
I have a grid in a XAML file in a WPF project. This MainGrid
I have been looking into MVVM design patterns with WPF for a project. I
I have a WPF C# project that I am working on and I have
I have a UniformGrid object in my WPF project that has 2 rows and
I have a WPF project (creating dll). The text boxes that added, (or the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.