-1

I had a very basic class with 1 property like this:

public class NodeItem { private object _Value; public object Value { get { return _Value; } set { _Value = value; } } } 

How can I define an event whenever Value property's value changed?

1
  • Define an event and in the setter for the property, set the value and also fire the event if the value has changed. You can use more than one line in the set {} Commented Dec 18, 2018 at 16:58

1 Answer 1

1

Here is simple example

public event PropertyChangedEventHandler PropertyChanged; private object _Value; public object Value { get { return _value; } set { _value = value; // Call OnPropertyChanged whenever the property is updated OnPropertyChanged(value); } } protected void OnPropertyChanged(object val) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(val)); } } 

Reference

for Subscribing to event

void MyEventSubscription(object sender, CustomEventArgs a) { // Do something useful here. } myClassInstance.PropertyChanged += MyEventSubscription; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. And how does I use that even in my code pls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.