绑定不按预期工作

时间:2015-09-17 17:50:07

标签: c# winforms data-binding

我正在做一个快速的&脏的WinForms应用程序。我已经定义了这个类:

class NotifyingRectangle : INotifyPropertyChanged {

    public int Left {
        get { return iLeft; }
        set {
            iLeft = value;
            onPropertyChanged( "Left" );
        }
    private int iLeft;

    // And so on for Top, Width & Height

    public event PropertyChangeEventHandler PropertyChanged;

    public NotifyingRectangle( int left, int top, int width, int height ) {
        iLeft = left;
        iTop = top;
        iWidth = width;
        iHeight = height;
    }

    protected virtual void OnPropertyChanged( string property ) {
        PropertyChangedEventHandler handler = PropertyChanged;
        if ( handler != null )
            handler( this, new PropertyChangedEventArgs( property ) );
    }
}

在我的表单中,我声明了一个类实例变量:

private NotifyingRectangle plateRectangle;

最后,我将类实例变量绑定到4个NumericUpDown控件:

nudLeft.DataBindings.Add( new Binding( "Value", plateRectangle, "Left", true, DataSourceUpdateMode.OnPropertyChanged ) );
nudTop.DataBindings.Add( new Binding( "Value", plateRectangle, "Top", true, DataSourceUpdateMode.OnPropertyChanged ) );
nudWidth.DataBindings.Add( new Binding( "Value", plateRectangle, "Width", true, DataSourceUpdateMode.OnPropertyChanged ) );
nudHeight.DataBindings.Add( new Binding( "Value", plateRectangle, "Height", true, DataSourceUpdateMode.OnPropertyChanged ) );

显示表单时,NumericUpDown控件会按预期显示类实例变量属性中的值。但是,当我通过单击向上或向下箭头更改其中一个控件的值时,该属性的值似乎不会更改。也就是说,我创建了一个ValueChanged事件处理程序,它由4个NumericUpDown控件调用,当我检查类实例变量的属性值时,就没有了变化

我做错了什么?如何使这些绑定双向工作?

0 个答案:

没有答案