如何在 c# 中进行数据绑定? [英] How can I do Databinding in c#?

查看:21
本文介绍了如何在 c# 中进行数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

<前>公共课车{公共名称{get;放;}}

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

我该怎么做?

在黑暗中拍摄:

<前>...汽车汽车 = 新汽车();TextEdit editBox = new TextEdit();editBox.DataBinding.Add("Name", car, "Car - Name");...

我收到以下错误

<块引用>

无法绑定到目标控件上的属性名称".

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

解决方案

你想要

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

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

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.

解决方案

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.

这篇关于如何在 c# 中进行数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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