C#DataBinding - 自动将更改的属性写入标签或文本框 [英] C# DataBinding - automatically writing changed property to label or textbox

查看:1071
本文介绍了C#DataBinding - 自动将更改的属性写入标签或文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一些关于DataBinding,大部分复杂的东西,比如SQL或者是什么XAML和东西。
所有我想我的程序要做的是,如果变量的值只是将它写入文本框或标签。 (使用WindowsForms)



到目前为止我有:

 命名空间DataBinding_Test 
{
public partial class Form1:Form
{

BindingSource bs = new BindingSource();
Class1 test = new Class1();

public Form1()
{
InitializeComponent();

test.name =Hello;
bs.DataSource = test;

label1.DataBindings.Add(new Binding(Text,bs,name,false,DataSourceUpdateMode.OnPropertyChanged));

}

private void button1_Click(object sender,EventArgs e)
{
test.name = textBox1.Text;
}
}
}

Class1只有一个公共属性名称。启动时lable1会显示我的Hello字符串。然后在按钮上单击名称属性将更改。在调试中,我看到bs的实际数据源包含新的属性值,但标签不会显示任何内容...



有没有任何真实的简单方法这个?



Backround是:周期性地会有RS232的传感器数据轮询。如果一个传感器的值更改,我想在标签或文本框中显示。现在一个backroundthreaded定时器将需要调用和东西来访问GUI线程;认为这将更容易与数据绑定,但似乎没有:P



感谢所有,伟大的网站,伟大的工作! :

解决方案

另一种让事情工作而不实现 INotifyPropertyChanged / p>

  class Class1 
{
private string name;

public string Name
{
get {return name; }
set
{
//检查是否分配相同的值。取决于你的业务逻辑
//这里是最简单的检查
if(Equals(name,value))
return;
name = value;
OnNameChanged();
}

public event EventHandler NameChanged;

protected virtual void OnNameChanged()
{
var handler = NameChanged;
if(handler!= null)
handler(this,EventArgs.Empty);
}
}
}

诀窍是让事件其名称与财产名称相结合,更改后缀,并在属性值更改时提高


I read some about DataBinding, mostly complicated things like SQL or whatever XAML and stuff. All I want my programm to do is, if the "value" of a variable changes just write it in a textbox or label. (using WindowsForms)

So far what I have:

namespace DataBinding_Test
{
    public partial class Form1 : Form
    {

    BindingSource bs = new BindingSource();
    Class1 test = new Class1();

    public Form1()
    {
        InitializeComponent();

        test.name = "Hello";
        bs.DataSource = test;

        label1.DataBindings.Add(new Binding("Text", bs, "name", false, DataSourceUpdateMode.OnPropertyChanged));

    }

    private void button1_Click(object sender, EventArgs e)
    {
        test.name = textBox1.Text;
    }
  }
}

Class1 just has a public property name. On startup lable1 will show my "Hello" string. Then on button click the name property will change. On debug I saw the actual DataSource of "bs" contains the new property value, but the label will not show anything...

Is there any realtivly easy way to do this?

The Backround is: periodically there will be a polling of sensor data throug RS232. If the value of one sensor changes I want to show this in label or textbox. Now a backroundthreaded timer will need invokes and stuff to access the GUI thread; thought this would be easier with databinding but seems not :P

Thanks to all, great site, great work! :)

解决方案

Another way to make things work without implementing INotifyPropertyChanged

class Class1
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            //Check if you are assigning the same value. Depends on your business logic
            //here is the simplest check
            if (Equals(name, value))
                return;
            name = value;
            OnNameChanged();
        }

        public event EventHandler NameChanged;

        protected virtual void OnNameChanged()
        {
            var handler = NameChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }
    }
}

The trick is to have event with the name combined by name of property and Changed suffix and to raise it whenever value of your property is changed

这篇关于C#DataBinding - 自动将更改的属性写入标签或文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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