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

时间:2011-07-15 15:07:02

标签: c# winforms binding c#-2.0 numericupdown

我需要twoway绑定configClass.RaMsize到numericUpDown。

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);
        }

1 个答案:

答案 0 :(得分:1)

我认为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