1

背景:

我有一个 numericupdown,它的“Value”属性绑定到一个对象的属性(当然是十进制)。对象的类实现 INotifyPropertyChanged,并且当属性更改其值时引发 propertychanged 事件,绑定设置为 updatemode.OnPropertyChanged。

private System.Windows.Forms.BindingSource myClassbindingSource;

在我的表单的 OnLoad 中,我有以下内容:

myClassbindingSource.Add( MyObject1 ); //(MyObject1 is an instance of MyClass)
base.OnLoad( e );

自动生成的绑定(在我将其 DataBinding 的高级属性“数据源更新模式”更改为“OnPropertyChanged”之后):

this.NumericUpDown1.DataBindings.Add(new System.Windows.Forms.Binding(
    "Value", this.myClassBindingSource, "MyProperty", true, 
    System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

我的课:

public class MyClass : INotifyPropertyChanged {

    private Decimal myValue;

    public Decimal MyProperty{
    get { return myvalue; }
    set { 
        myvalue = value;
        NotifyPropertyChanged( "MyProperty" );
    }

    ...

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged( String info ) {
      var pceHandler = PropertyChanged;
      var pceArgs = new PropertyChangedEventArgs( info );
      if( pceHandler != null )
        pceHandler( this, pceArgs );
    }
}

问题:

现在,当对象的属性从代码的其他部分更改时,NumericUpdown 不会显示其新值。

当我调试我的代码时,我可以看到,当属性更改时,PropertyChanged 事件会触发并且我的 NUD 的“Value”属性会获得正确的值,但“Text”属性的默认值是“0.000”。

还有其他控件的绑定工作正常,例如复选框与布尔值、标签与字符串。但不是我的 NUD 绑定到十进制。

问题:

当“Value”属性更新时,如何让“Text”属性更新,从而显示正确的值?或者更确切地说,为什么“文本”属性不更新?是否有可以控制如何处理的属性?

4

0 回答 0