DependencyProperties: PropertyChangedCallBack is called only once

I created a control, obtained from Canvas, which should build a live diagram, given values ​​that are passed through the binding to DependencyProperty. The simplified version is as follows:

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 linked the control as follows:

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

My ViewModel implements INotifyPropertyChangedand correctly calls PropertyChanged. If I bind MyViewModelPropertyto a text box, it will be updated correctly every time. Only if I attach it to my own control does mine ValueChangedCallBackonly get called once when the page loads, and then never again.

What I do not see here? Thanks for any help!

Solved: I should not explicitly specify Valuein the callback.

+3
3

Value Value change. . , , ​​ ?

+5

TwoWay?

+1

Should you not use DependencyProperty.Register instead of DependencyProperty.RegisterAttached?

+1
source

All Articles