如何在 c# 中进行数据绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/115328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:24:56  来源:igfitidea点击:

How can I do Databinding in c#?

提问by Xerx

I have the following class

我有以下课程

public class Car
{
   public Name {get; set;}
}

and I want to bind this programmatically to a text box.

我想以编程方式将其绑定到文本框。

How do I do that?

我怎么做?

Shooting in the dark:

暗中拍摄:

...
Car car = new Car();
TextEdit editBox = new TextEdit();
editBox.DataBinding.Add("Name", car, "Car - Name");
...

I get the following error

我收到以下错误

"Cannot bind to the propery 'Name' on the target control.

“无法绑定到目标控件上的属性“名称”。

What am I doing wrong and how should I be doing this? I am finding the databinding concept a bit difficult to grasp coming from web-development.

我做错了什么,我该怎么做?我发现来自网络开发的数据绑定概念有点难以掌握。

采纳答案by ageektrapped

You want

你要

editBox.DataBindings.Add("Text", car, "Name");

The first parameter is the name of the property on the control that you want to be databound, the second is the data source, the third parameter is the property on the data source that you want to bind to.

第一个参数是要绑定到的控件上的属性名称,第二个是数据源,第三个参数是要绑定到的数据源上的属性。

回答by Danimal

Without looking at the syntax, I'm pretty sure it's:

不看语法,我很确定它是:

editBox.DataBinding.Add("Text", car, "Name");

回答by Danimal

You're trying to bind to the "Name" of the TextEdit control. The name is used for accessing the control programmatically, and cannot be bound against. You should be binding against the Text of the control.

您正在尝试绑定到 TextEdit 控件的“名称”。该名称用于以编程方式访问控件,不能绑定。您应该绑定到控件的文本。

回答by TcKs

Try:

尝试:

editBox.DataBinding.Add( "Text", car", "Name" );

回答by itsmatt

I believe that

我相信

editBox.DataBindings.Add(new Binding("Text", car, "Name"));

editBox.DataBindings.Add(new Binding("Text", car, "Name"));

should do the trick. Didn't try it out, but I think that's the idea.

应该做的伎俩。没有尝试过,但我认为这就是想法。

回答by Romain Verdier

editBox.DataBinding.Add("Text", car, "Name");

First arg is the name of the control property, the second is the object to bind, and the last, the name of the object property you want to use as the data source.

第一个 arg 是控件属性的名称,第二个是要绑定的对象,最后一个是要用作数据源的对象属性的名称。

回答by John Hunter

You are quite close the data bindings line would be

您非常接近数据绑定行

editBox.DataBinding.Add("Text", car, "Name");

This first parameter is the property of your editbox object that will be data bound. The second parameter is the data source you are binding to and the last parameter is the property on the data source that you want to bind to.

第一个参数是将被数据绑定的编辑框对象的属性。第二个参数是您要绑定到的数据源,最后一个参数是您要绑定到的数据源上的属性。

Bear in mind that the data binding is one way so if you change the edit box then the car object gets updated but if you change the car name directly the edit box is not updated.

请记住,数据绑定是一种方式,因此如果您更改编辑框,则汽车对象会更新,但如果您直接更改汽车名称,则不会更新编辑框。

回答by tofo

The following is generic class that can be used as a property and implements INotifyPropertyChanged used by bound controls to capture changes in the property value.

以下是可用作属性的泛型类,并实现绑定控件使用的 INotifyPropertyChanged 以捕获属性值的更改。

public class NotifyValue<datatype> : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    datatype _value;
    public datatype Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Value"));
        }
    }

}

It can be declared like this:

它可以这样声明:

public NotifyValue<int> myInteger = new NotifyValue<int>();

and assigned to a textbox like this

并分配给这样的文本框

Textbox1.DataBindings.Add(
    "Text", 
    this, 
    "myInteger.Value", 
    false, 
    DataSourceUpdateMode.OnPropertyChanged
);

..where "Text" is the property of the textbox, 'this' is current Form instance.

..其中“Text”是文本框的属性,“this”是当前的 Form 实例。

A class does not have to inherit the INotifyPropertyChanged class. Once you declare an event of type System.ComponentModel.PropertyChangedEventHandler the class change event will be subscribed to by the controls databinder

类不必继承 INotifyPropertyChanged 类。一旦你声明了一个 System.ComponentModel.PropertyChangedEventHandler 类型的事件,类更改事件将被控件数据绑定器订阅

回答by Graviton

Using C# 4.6 syntax:

使用 C# 4.6 语法:

editBox.DataBinding.Add(nameof(editBox.Text), car, nameof(car.Name));

if car is null, then the above code will fail in a more conspicuous way than using literal string to represent the datamemberof car

如果车是空的话,上面的代码将在更加突出的方式失败不是使用文本字符串来表示datamembercar

回答by Ramgy Borja

it's

它是

 this.editBox.DataBindings.Add(new Binding("Text", car, "Name"));