James Joshua Street
James Joshua Street

Reputation: 3409

how to provide change notification for a property when a subproperty changes?

This is such a basic question, but I don't think I've done this before despite having bound so many properties. I originally was planning to bind a class called TimeScale to various objects.
In class A we have a dependency property that I want to call change notification on. However, change notification is not done manually through this class.

public TimeScale AxisTimeScale
{
    get { return (TimeScale)GetValue(AxisTimeScaleProperty); }
    set { SetValue(AxisTimeScaleProperty, value); }
}

public static readonly DependencyProperty AxisTimeScaleProperty =
    DependencyProperty.Register("AxisTimeScale",
        typeof(TimeScale), typeof(SignalPanel),
        new FrameworkPropertyMetadata(new TimeScale()));

this binds to source class B

private class B
{
    private TimeScale _GraphTimeScale;

    public TimeScale GraphTimeScale
    {
        get { return _GraphTimeScale; }
        set
        {
            if (value != _GraphTimeScale)
            {
                _GraphTimeScale = value;
                OnPropertyChanged("GraphTimeScale");
            }
        }
    }
}

Looking at it again I guess all I really want is to call propertychanged on a dependency property, but since I didn't implement Inotifypropertychanged, I am wondering how i do that.

I think DependencyObject already implements Inotifypropertychanged, so I have access to this:

OnPropertyChanged(new DependencyPropertyChangedEventArgs(property, old value, new value));

However, inserting the same object into both the old value and new value slots results in the PropertyChanged event not firing (I assume the implementation checks whether the two values are the same before firing the event). I want to avoid creating a new object if possible. I guess one option is to override OnPropertyChanged. Nope that also requires me to have a dependency propertychanged event args.

Upvotes: 2

Views: 627

Answers (2)

d.moncada
d.moncada

Reputation: 17402

Update

OnPropertyChanged("TimeScale");

to

OnPropertyChanged("GraphTimeScale");

Or,

you can wrap the TimeScale class with an ObservableObject so that you can subscribe to object change events and raise them from there.

More info: http://msdn.microsoft.com/en-us/library/ff653818.aspx

Upvotes: 1

Stuart Grassie
Stuart Grassie

Reputation: 3073

Subscribe to the PropertyChanged notification of NumberOfUnits, and then raise OnPropertyChanged("GraphTimeScale") in the property changed event handler.

Would be interested if there is a better way though.

Upvotes: 0

Related Questions