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

时间:2021-04-16 15:05:51

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.

我做错了什么,我应该怎么做?我发现数据绑定概念来自Web开发有点难以理解。

10 个解决方案

#1


49  

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.

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

#2


11  

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

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

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

#3


7  

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是控件属性的名称,第二个是要绑定的对象,最后一个是要用作数据源的对象属性的名称。

#4


6  

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.

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

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.

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

#5


3  

Try:

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

#6


3  

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.

应该做的伎俩。没试过,但我认为这是个主意。

#7


1  

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控件的“名称”。该名称用于以编程方式访问控件,不能绑定。您应该绑定控件的文本。

#8


0  

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.

..where“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的事件后,控件数据块将订阅类更改事件

#9


0  

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 datamember of car

如果car为null,则上述代码将以比使用文字字符串表示汽车数据更明显的方式失败

#10


0  

it's

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

#1


49  

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.

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

#2


11  

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

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

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

#3


7  

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是控件属性的名称,第二个是要绑定的对象,最后一个是要用作数据源的对象属性的名称。

#4


6  

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.

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

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.

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

#5


3  

Try:

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

#6


3  

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.

应该做的伎俩。没试过,但我认为这是个主意。

#7


1  

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控件的“名称”。该名称用于以编程方式访问控件,不能绑定。您应该绑定控件的文本。

#8


0  

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.

..where“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的事件后,控件数据块将订阅类更改事件

#9


0  

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 datamember of car

如果car为null,则上述代码将以比使用文字字符串表示汽车数据更明显的方式失败

#10


0  

it's

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