Mike
Mike

Reputation: 3294

Observing NotifyPropertyChanged and responding to it

I have a collection of Field objects and each Field object implements INotifyPropertyChanged. The field object has various properties inside it, however I have one property called IsApproved which I need to listen for any changes. My interest is that if that boolean flag is set or unset, I need to get notified or basically I need to respond to that event(the property will be set or unset by the UI via WPF binding). Can I use Reactive Extensions for this, or is it overkill? If not what would you recommend?

Code:

public class Field : INotifyPropertyChanged
{
  private bool _isApproved;
        public bool IsApproved
        {
            get { return _isApproved; }

            set
            {
                if (_isApproved == value)
                    return;
                _isApproved = value;
                RaisePropertyChanged(() => IsApproved);
            }
        }

///has lots of other properties.
}

In my viewmodel, I have a collection of Fields, and I need to observe on those to see when the IsApproved property is set or unset on any or all of them. How can I do that?

Edit: I have the Fields collection an an observable collection, which is bound to an itemscontrol. Each item inside the field is rendered using a datatemplate and the template is selected using a template selector. My IsApproved property is bound to a checkbox in each of the datatemplate. I have a button on my page, and onclicking that button all Approved checkboxes should be set.I have another button which tracks the state of all Approvals, basically if everything is approved, then that button(submit) should be enabled and if any of the field is not approved then that button should be disabled.

Upvotes: 1

Views: 207

Answers (2)

Adi Lester
Adi Lester

Reputation: 25211

If all you need to do is update the UI when the property changes, you can simply bind to that property and you're done (as was also mentioned by @sircodesalot):

<Checkbox IsChecked="{Binding IsApproved}" />

If however you want to listen for changes programatically, you can register to the PropertyChanged event like this:

myField.PropertyChanged += (sender, e) =>
{
    if (e.PropertyName == "IsApproved")
    {
        // Do stuff
    }
}

Upvotes: 2

sircodesalot
sircodesalot

Reputation: 11439

If you have a binding on the window, like say a checkbox:

<Checkbox IsChecked="{Binding IsApproved}" />

You should get a notification when the property changes.

That said, it is possible also to build Dependency Properties that allow for a callback when the property has been changed. This is slightly different from what your example seems to be indicating (but it's worth knowing, because its still closely related). Consider:

public partial class InfoBox : UserControl {

    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(InfoBox),
        new FrameworkPropertyMetadata(TextPropertyChanged));


    public static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) {
        InfoBox infoBox = (InfoBox)sender;
        infoBox.ContentText.Content = args.NewValue;
    }

    public String Text {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

Essentially the class defines a TextProperty, and creates an accessor (bottom). When I register the TextProperty with WPF (DependencyProperty.Register, the last parameter allows me to specify some configuration options (FrameworkPropertyMetadata), which you can use to specify a callback that gets fired when the property changes.

Upvotes: 0

Related Questions