关于数据绑定的问题 - 将绑定控件绑定到业务对象的成员。 [英] Question about Databinding - binding controls to members of my business object.

查看:76
本文介绍了关于数据绑定的问题 - 将绑定控件绑定到业务对象的成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有一个MyClass实例,它具有属性

MyText1和MyText2,并将MyText1和MyText2的值设置为''A''

和''B''在它的构造函数中。


我们也假设我有一个带有textBox1,textBox2,btnShowValue的表单,

和btnNew。


我们也假设我有这个代码。


//表格成员

MyClass MyObject;


//在表单加载方法中:

{

MyObject = new MyClass();


textBox1.DataBindings.Add(" Text",MyObject," MyText1");

textBox2.DataBindings.Add(" Text", MyObject,MyText2;

}


//在btnShowValue中单击方法

{

MessageBox.Show(MyObject.MyText1);

MessageBox.Show(MyObject.MyText2);

}


/ / in btnNew点击方法

{

MyObject = new MyClass();

}

如果我要运行这个程序,我可以开始输入Textbox1和

Textbox2。然后,如果我点击btnShowValue,我会看到

MessageBoxes,其中包含我刚输入的值,就像

文本框绑定到对象的属性一样。


但是,如果我点击btnNew,我希望Textbox1和Textbox2转换为默认值A和B ''。


他们没有。他们保持不变。无论你在文本框中输入什么内容,点击btnShowValues都会给你

''A''和''B''。


我的问题是为什么?我在这里缺少什么?

我如何更改此代码以使其按照我想要的方式工作?

Let''s suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to ''A''
and ''B'' in it''s constructor.

Let''s also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let''s also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBindings.Add("Text", MyObject, "MyText1");
textBox2.DataBindings.Add("Text", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show(MyObject.MyText1);
MessageBox.Show(MyObject.MyText2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values ''A'' and ''B''.

They do not. They stay the same. And clicking btnShowValues gives you
''A'' and ''B'' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?

推荐答案

Michael,


当你将MyObject引用传递给Add方法时,它会记住

引用,但它什么都不知道关于变量或它何时发生变化。


您需要做的是删除绑定,然后添加一个新的绑定,

并引用新的类实例。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP] < br $> b $ b - mv*@spam.guard.caspershouse.com


< Mi ************ @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Michael,

When you pass the MyObject reference to the Add method, it will remember
the reference, but it knows nothing about the variable or when it changes.

What you would have to do is remove the binding, and then add a new one,
with a reference to the new class instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Mi************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
让我们假设我有一个MyClass实例,它具有属性
MyText1和MyText2,并将MyText1和MyText2的值设置为''A''
和''B ''在它'的构造函数。

我们也假设我有一个带有textBox1,textBox2,btnShowValue,
和btnNew的表单。

让我们我也假设我有这段代码。

//表格成员
MyClass MyObject;

//表单加载方法:
{
MyObject = new MyClass();

textBox1.DataBindings.Add(" Text",MyObject," MyText1");
textBox2.DataBindings.Add("文本",MyObject," MyText2");
}
//在btnShowValue中单击方法
{MessageBox.Show(MyObject.MyText1);
MessageBox.Show(MyObject.MyText2);
}
//在btnNew中单击方法
{
MyObject = new MyClass();
}

如果我要运行这个程序,我可以开始输入Textbox1和
Textbox2。然后,如果我点击btnShowValue,我会看到
MessageBoxes,其中包含我刚输入的值,因为
文本框绑定到对象的属性。

但是,如果我点击btnNew,我希望Textbox1和Textbox2恢复到默认值A和B。

他们没有。他们保持不变。无论您在文本框中输入什么内容,点击btnShowValues都会从现在开始为您提供A和B。

我的问题是为什么?我在这里错过了什么?
我如何更改此代码以使其按照我希望的方式工作?
Let''s suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to ''A''
and ''B'' in it''s constructor.

Let''s also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let''s also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBindings.Add("Text", MyObject, "MyText1");
textBox2.DataBindings.Add("Text", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show(MyObject.MyText1);
MessageBox.Show(MyObject.MyText2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values ''A'' and ''B''.

They do not. They stay the same. And clicking btnShowValues gives you
''A'' and ''B'' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?



替代以上是绑定不是对象,而是绑定到一个

BindingSource;这可以作为UI和一个或多个对象之间的代理

(可能允许光标导航,但这是'

场景的一部分。)


你所做的只是改变BindingSource中的对象,让你在设计师中做你的绑定(如果你愿意的话)。


我之前向这个小组发布了一个完整的工作示例 - 12月8日

2005,标题为Re:将数据绑定到表单控件。


Marc


< Mi ************ @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
An alternative to the above is to bind not the the object, but to a
BindingSource; this acts as a proxy between the UI and one-or-more objects
(potentially allowing cursor navigation, but that''s an aside for this
scenario).

All you then do is change the object in the BindingSource, leaving you to do
your bindings in the designer (if you so choose).

I posted a full working example of this to this group previously - 8th Dec
2005, titled "Re: Binding data to form controls".

Marc

<Mi************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
让我们假设我有一个MyClass实例,它具有属性
MyText1和MyText2,并将MyText1和MyText2的值设置为''A''
和''B ''在它'的构造函数。

我们也假设我有一个带有textBox1,textBox2,btnShowValue,
和btnNew的表单。

让我们我也假设我有这段代码。

//表格成员
MyClass MyObject;

//表单加载方法:
{
MyObject = new MyClass();

textBox1.DataBindings.Add(" Text",MyObject," MyText1");
textBox2.DataBindings.Add("文本",MyObject," MyText2");
}
//在btnShowValue中单击方法
{MessageBox.Show(MyObject.MyText1);
MessageBox.Show(MyObject.MyText2);
}
//在btnNew中单击方法
{
MyObject = new MyClass();
}

如果我要运行这个程序,我可以开始输入Textbox1和
Textbox2。然后,如果我点击btnShowValue,我会看到
MessageBoxes,其中包含我刚输入的值,因为
文本框绑定到对象的属性。

但是,如果我点击btnNew,我希望Textbox1和Textbox2恢复到默认值A和B。

他们没有。他们保持不变。无论您在文本框中输入什么内容,点击btnShowValues都会从现在开始为您提供A和B。

我的问题是为什么?我在这里缺少什么?
以及如何更改此代码以使其按照我希望的方式工作?
Let''s suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to ''A''
and ''B'' in it''s constructor.

Let''s also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let''s also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBindings.Add("Text", MyObject, "MyText1");
textBox2.DataBindings.Add("Text", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show(MyObject.MyText1);
MessageBox.Show(MyObject.MyText2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values ''A'' and ''B''.

They do not. They stay the same. And clicking btnShowValues gives you
''A'' and ''B'' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?



感谢您的支持响应。


好​​像BindingSource是2005年的新手。我的项目使用了

a .Net API,它是在1.1中创建的,在2.0中不兼容项目,

因此限制我使用VS 2003.


2003年与BindingSource相比的任何东西?

Thanks for the responses.

It appears as though the BindingSource is new to 2005. My project uses
a .Net API that was created in 1.1 and not compatible in a 2.0 project,
therefore restricting me to use VS 2003.

Anything comparable to a BindingSource in 2003?


这篇关于关于数据绑定的问题 - 将绑定控件绑定到业务对象的成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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