如何通过两种方式将numericUpDown绑定到成员类 [英] How make two way binding numericUpDown to member class

查看:129
本文介绍了如何通过两种方式将numericUpDown绑定到成员类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要双向绑定configClass.RaMsize到numericUpDown.

I need twoway binding configClass.RaMsize to 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);
        }

推荐答案

我认为ConfigClass看起来像:

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

因此更改 control.DataBindings.Add(propertyName, dataSource, dataMember); 到:

So change control.DataBindings.Add(propertyName, dataSource, dataMember); to:

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

这篇关于如何通过两种方式将numericUpDown绑定到成员类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆