DependencyProperties:PropertyChangedCallBack只调用一次

Phi*_*ier 3 c# data-binding silverlight events windows-phone-7

我创建了一个控件,派生自Canvas,应该绘制一个实时图,给定通过绑定传递给DependencyProperty的值.简化版本如下:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我像这样绑定控件:

<mystuff:Plotter Value="{Binding MyViewModelProperty}" Height="50" Width="200" />
Run Code Online (Sandbox Code Playgroud)

我的ViewModel实现INotifyPropertyChangedPropertyChanged正确调用.如果我绑定MyViewModelProperty到文本框,则每次都会正确更新.只有当我将它绑定到我自己的控件时,我ValueChangedCallBack才会在页面加载时调用一次,然后再也不会.

我在这里看不到什么?谢谢你的帮助!

解决:我没有必要Value在回调中明确设置.

Nic*_*s W 5

您在属性值更改的回调上设置属性值.在任何情况下都没有多大意义.但是,本地设置值是否会覆盖绑定值,从而导致不再在依赖项属性上设置绑定?