Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperties: PropertyChangedCallBack only called once

I created a control, derived from Canvas, that should plot a live diagram, given values that are passed via a binding to a DependencyProperty. The simplified version is this:

public class Plotter : Canvas
{
    public float Value { get { return (float)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(float), typeof(Plotter),
        new PropertyMetadata(0f, new PropertyChangedCallback(ValueChangedCallBack)));

    public static void ValueChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        Plotter plotter = (Plotter)property;
        plotter.Value = (float)args.NewValue; //<-- Removed this line to get it to work

        // Actually draw the value into the canvas geometry
        plotter.PlotValue(plotter.Value);
    }
}

I bound the control like this:

<mystuff:Plotter Value="{Binding MyViewModelProperty}" Height="50" Width="200" />

My ViewModel implements INotifyPropertyChanged and calls PropertyChanged correctly. If I bind MyViewModelProperty to a textbox, it correctly updates every time. Only if I bind it to my own control, my ValueChangedCallBack is only called once as the page is loaded, and then never again.

What am I not seeing here? Thanks for any help!

Solved: I dont have to set the Value explicitly in the callback.

like image 311
Philip Daubmeier Avatar asked Sep 01 '26 21:09

Philip Daubmeier


1 Answers

You set the property Value on the callback for the property Value changing. That doesn't make much sense in any case. But is that locally set value overriding the binding value, causing your binding to no longer be set on the dependency property?

like image 145
Nicholas W Avatar answered Sep 03 '26 13:09

Nicholas W