如何使numericUpDown双向绑定到成员类

本文关键字:绑定 成员类 何使 numericUpDown | 更新日期: 2023-09-27 18:02:07

我需要双向绑定configClass。

BindField(this.upDownRamSize, "Value", configClass, "RaMsize");//all right
this.upDownRamSize.Value = 1213;// configClass.RaMsize - not change - it's bad!

方法:

public static void BindField(Control control, string propertyName,
               object dataSource, string dataMember)
        {
            Binding bd;
            for (int index = control.DataBindings.Count - 1; (index == 0); index--)
            {
                bd = control.DataBindings[index];
                if (bd.PropertyName == propertyName)
                    control.DataBindings.Remove(bd);
            }
            control.DataBindings.Add(propertyName, dataSource, dataMember);
        }

如何使numericUpDown双向绑定到成员类

我假设ConfigClass看起来像:

public class ConfigClass
{
    public decimal RaMsize { get; set; }
}

改变 control.DataBindings.Add(propertyName, dataSource, dataMember);:

control.DataBindings.Add(propertyName, dataSource, dataMember, false, DataSourceUpdateMode.OnPropertyChanged);
测试:

BindField(this.upDownRamSize, "Value", configClass, "RaMsize");
this.upDownRamSize.Value = 1213;// configClass.RaMsize will also have 1213