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 6975737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:24:25+00:00 2026-05-27T17:24:25+00:00

I am implementing the observer pattern for our application – currently playing around with

  • 0

I am implementing the observer pattern for our application – currently playing around with the RX Framework.

I currently have an example that looks like this:

Observable.FromEventPattern<PropertyChangedEventArgs>(Instance.Address, "PropertyChanged")
    .Where(e => e.EventArgs.PropertyName == "City")
    .ObserveOn(Scheduler.ThreadPool)
    .Subscribe(search => OnNewSearch(search.EventArgs));

(I have a similar one for “PropertyChanging”)

The EventArgs don’t give me much. What I would like is an extension of the EventArgs that would give me the ability to see the previous and new values, as well as the ability to mark the event in the ‘changing’ listener, such that the change wouldn’t actually persist. How can this be done? Thanks.

  • 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-27T17:24:26+00:00Added an answer on May 27, 2026 at 5:24 pm

    I think that it comes down to how you implement the INotifyPropertyChanging and INotifyPropertyChanged interfaces.

    The PropertyChangingEventArgs and PropertyChangedEventArgs classes unfortunately don’t provide a before and after value of the property or the ability to cancel the change, but you can derive your own event args classes that do provide that functionality.

    First, define the following event args classes. Notice that these derive from the PropertyChangingEventArgs class and PropertyChangedEventArgs class. This allows us to pass these objects as arguments to the PropertyChangingEventHandler and PropertyChangedEventHandler delegates.

    class PropertyChangingCancelEventArgs : PropertyChangingEventArgs
    {
        public bool Cancel { get; set; }
    
        public PropertyChangingCancelEventArgs(string propertyName)
            : base(propertyName)
        {
        }
    }
    
    class PropertyChangingCancelEventArgs<T> : PropertyChangingCancelEventArgs
    {
        public T OriginalValue { get; private set; }
    
        public T NewValue { get; private set; }
    
        public PropertyChangingCancelEventArgs(string propertyName, T originalValue, T newValue)
            : base(propertyName)
        {
            this.OriginalValue = originalValue;
            this.NewValue = newValue;
        }
    }
    
    class PropertyChangedEventArgs<T> : PropertyChangedEventArgs
    {
        public T PreviousValue { get; private set; }
    
        public T CurrentValue { get; private set; }
    
        public PropertyChangedEventArgs(string propertyName, T previousValue, T currentValue)
            : base(propertyName)
        {
            this.PreviousValue = previousValue;
            this.CurrentValue = currentValue;
        }
    }
    

    Next, you would need to use these classes in your implementation of the INotifyPropertyChanging and INotifyPropertyChanged interfaces. An example of an implementation is the following:

    class Example : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event PropertyChangingEventHandler PropertyChanging;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected bool OnPropertyChanging<T>(string propertyName, T originalValue, T newValue)
        {
            var handler = this.PropertyChanging;
            if (handler != null)
            {
                var args = new PropertyChangingCancelEventArgs<T>(propertyName, originalValue, newValue);
                handler(this, args);
                return !args.Cancel;
            }
            return true;
        }
    
        protected void OnPropertyChanged<T>(string propertyName, T previousValue, T currentValue)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs<T>(propertyName, previousValue, currentValue));
        }
    
        int _ExampleValue;
    
        public int ExampleValue
        {
            get { return _ExampleValue; }
            set
            {
                if (_ExampleValue != value)
                {
                    if (this.OnPropertyChanging("ExampleValue", _ExampleValue, value))
                    {
                        var previousValue = _ExampleValue;
                        _ExampleValue = value;
                        this.OnPropertyChanged("ExampleValue", previousValue, value);
                    }
                }
            }
        }
    }
    

    Note, your event handlers for the PropertyChanging and PropertyChanged events will still need to take the original PropertyChangingEventArgs class and PropertyChangedEventArgs class as parameters, rather than a more specific version. However, you will be able to cast the event args objects to your more specific types in order to access the new properties.

    Below is an example of event handlers for these events:

    class Program
    {
        static void Main(string[] args)
        {
            var exampleObject = new Example();
    
            exampleObject.PropertyChanging += new PropertyChangingEventHandler(exampleObject_PropertyChanging);
            exampleObject.PropertyChanged += new PropertyChangedEventHandler(exampleObject_PropertyChanged);
    
            exampleObject.ExampleValue = 123;
            exampleObject.ExampleValue = 100;
        }
    
        static void exampleObject_PropertyChanging(object sender, PropertyChangingEventArgs e)
        {
            if (e.PropertyName == "ExampleValue")
            {
                int originalValue = ((PropertyChangingCancelEventArgs<int>)e).OriginalValue;
                int newValue = ((PropertyChangingCancelEventArgs<int>)e).NewValue;
    
                // do not allow the property to be changed if the new value is less than the original value
                if(newValue < originalValue)
                    ((PropertyChangingCancelEventArgs)e).Cancel = true;
            }
    
        }
    
        static void exampleObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ExampleValue")
            {
                int previousValue = ((PropertyChangedEventArgs<int>)e).PreviousValue;
                int currentValue = ((PropertyChangedEventArgs<int>)e).CurrentValue;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a small application (observer) that needs to attach itself to the
I have met an interesting problem while implementing the Observer pattern with C++ and
I'm working on implementing an observer design pattern with a notification object that I
I am implementing MKMapView based application. In that I am using an observer when
I am having problems with implementing Observer pattern in my project. The project has
I was thinking about implementing a logic similar to observer pattern on my website,
I'm currently working on an Android application. I wish to have a GridView on
I've been implementing a barebones observer pattern and am stuck on a somewhat cryptic
I was reading around about the Observer pattern, and found a dated article .
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks

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.