数据绑定到业务对象 [英] Databinding to a business object

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

问题描述




我有一个对象,我绑定到一个文本框,这个对象暴露了一个

布尔字段,我实现了一个格式化事件处理程序和绑定对象的
解析事件处理程序,我将bool

值转换为一些有意义的文本。


ie


Binding b = new

Binding(" Text",AppOptions.Instance," RemoteDataRetr ieved");


b.Format + = new ConvertEventHandler(RemoteDataRetrievedToString);

b.Parse + = new ConvertEventHandler(StringToRemoteDataRetrieved);

textBox1.DataBindings.Add (b);


private void RemoteDataRetrievedToString(object sender,

ConvertEventArgs e)

{

if(e.DesiredType!= typeof(string))return;

if(e.Value.GetType()!= typeof(bool))return;

e .Value =(bool)e.Value? 远程数据加载 :远程数据是

未加载;

}


private void StringToRemoteDataRetrieved(对象发送者,

ConvertEventArgs e)

{

if(e.DesiredType!= typeof(bool))return;

if(e。 Value.GetType()!= typeof(string))return;

e.Value =(string)e.Value ==" Remote Data Loaded";

}


(对不起,如果格式很奇怪,我的新闻阅读器很奇怪)


这实际上似乎工作正常,除非我改变我的业务

对象'的代码属性,文本框不更新。我必须

遗漏了一些明显的东西。


问候蒂姆。

Hi,

I have an object that I am binding to a text box, this object exposes a
boolean field, and I have implemented a format event handler and a
parse event handler for the binding object, where I convert the bool
value to some meaningful text.

i.e.

Binding b = new
Binding("Text",AppOptions.Instance,"RemoteDataRetr ieved");

b.Format += new ConvertEventHandler(RemoteDataRetrievedToString);
b.Parse += new ConvertEventHandler(StringToRemoteDataRetrieved);
textBox1.DataBindings.Add(b);

private void RemoteDataRetrievedToString(object sender,
ConvertEventArgs e)
{
if (e.DesiredType != typeof(string) ) return;
if (e.Value.GetType() != typeof(bool) ) return;
e.Value = (bool) e.Value ? "Remote Data Loaded" : "Remote Data is
not Loaded";
}

private void StringToRemoteDataRetrieved(object sender,
ConvertEventArgs e)
{
if (e.DesiredType != typeof(bool) ) return;
if (e.Value.GetType() != typeof(string) ) return;
e.Value = (string) e.Value == "Remote Data Loaded";
}

(sorry if the formatting is bizarre, my newsreader is wrapping Weirdly)

This actually seems to work ok, except when I change my business
object''s property in code, the textbox does not update. I must be
missing something obvious.

Regards Tim.

推荐答案

你好蒂姆,


感谢您在小组中发帖。


根据我的理解,现在的问题是:你使用简单绑定

将对象绑定到文本框。但是,如果以编程方式更改值

,相应的文本框将不会显示新数据。它是

对吗?


我从这个目录下的WinForm快速入门示例开始如果你安装了
.NET框架示例:

C:\Program Files \ Microsoft Visual Studio

.NET\FrameworkSDK\Samples\QuickStart \ winforms\samp les\data \\ \\ simplebinding\cs


在这个项目中,我在窗体上添加了一个按钮,在按钮的OnClick处理程序中,我添加了代码:

custList [this.BindingContext [custList] .Position + 1] .FirstName =" Yanhong";


在这种情况下,textbox的数据赢了'不会精神焕发。只有在

之后,您单击下一步,然后返回,才会显示新值。为了让
让TextBox显示最新数据,我们可以添加以下代码:

textBoxFirstName.Text =

custList [this。 BindingContext [custList] .Position + 1] .FirstName;


此外,在代码中,我们可以将文本框的文本字段设置为新值

首先,然后将其更新为upderlying绑定源。

[C#]

this.textBox1.Text =" XXXX" ;; //设置值

this.textBox1.DataBindings [" Text"]。BindingManagerBase.EndCurrentEdit();

//结束编辑


希望有所帮助。


祝你好运,

Yanhong Huang

微软社区支持


安全! - www.microsoft.com/security

发布是按原样提供的。没有保证,也没有授予任何权利。

Hello Tim,

Thanks for posting in the group.

Based on my understanding, now the problem is: You use simple binding to
bind a object to a text box. However, if you change the value
programmatically, the corresponding textbox won''t show the new data. Is it
right?

I started from our WinForm quick start sample at this directory if you
installed .NET framework samples:
C:\Program Files\Microsoft Visual Studio
.NET\FrameworkSDK\Samples\QuickStart\winforms\samp les\data\simplebinding\cs

In this project, I added a button to the form and in the OnClick handler of
that button, I added codes:
custList[this.BindingContext[custList].Position+1].FirstName = "Yanhong";

Under this situation, the data of textbox won''t be refreshed. Only after
you click next, and then go back, the new value can be shown. In order to
let TextBox to show the newest data, we could add the following code:
textBoxFirstName.Text =
custList[this.BindingContext[custList].Position+1].FirstName;

Besides, in the code, we can set the text field of textbox to the new value
first and then update it to the upderlying binding source.
[C#]
this.textBox1.Text = "XXXX"; //set the value
this.textBox1.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
//end the edit

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Gents,


我认为格式和解析Binding对象的事件

可能是一个非常好的指针,指示何时从

汇总数据或将数据推送到绑定数据源。


据我记忆,这些事件中的每一个都有一个相关的

条件列表,在这个条件下它被触发,所以这应该传达一个关于
$的一般概念b $ b数据绑定机制如何在内部工作。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity测试工作室
http://www.x-unity .net / teststudio.aspx

将单元测试的强大功能带到VS .NET IDE


Yan-Hong Huang [MSFT] " < YH ***** @ online.microsoft.com>在消息中写道

news:
Gents,

I think the description of the Format and Parse events of the Binding object
might be a very good pointer on when exactly the data is being pooled from
or pushed to the bound data source.

As far as I remember, each of these events has an associated list of
conditions under which it is fired, so this should convey a general idea on
how the data binding mechanics work internally.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:


M ************** @ cpmsftngxa07.phx.gbl ...
M**************@cpmsftngxa07.phx.gbl...
你好Tim,

感谢您在小组中发帖。

根据我的理解,现在的问题是:你使用简单的绑定到
将对象绑定到文本框。但是,如果以编程方式更改值,则相应的文本框不会显示新数据。它是对的吗?

如果你安装了.NET框架示例,我从这个目录的WinForm快速入门示例开始:
C:\Program Files \\ \\ _Microsoft Visual Studio
NET\FrameworkSDK \Samples \QuickStart \winforms\sampl es\data \simplebinding\cs

在这个项目中,我添加了一个按钮在表单和该按钮的OnClick处理程序
中,我添加了代码:
custList [this.BindingContext [custList] .Position + 1] .FirstName =" Yanhong";
<在这种情况下,文本框的数据不会被刷新。只有在您单击下一步然后返回后,才能显示新值。为了让TextBox显示最新数据,我们可以添加以下代码:
textBoxFirstName.Text =
custList [this.BindingContext [custList] .Position + 1] .FirstName;

此外,在代码中,我们可以先将textbox的文本字段设置为新的
值,然后将其更新为upderlying绑定源。
[C#]
this.textBox1.Text =" XXXX" ;; //设置值
this.textBox1.DataBindings [" Text"]。BindingManagerBase.EndCurrentEdit();
//结束编辑

希望有所帮助。 />
致以最诚挚的问候,
黄艳红
微软社区支持

安全! - www.microsoft.com/security
此帖子提供 ;按原样没有保证,也没有
权利。
Hello Tim,

Thanks for posting in the group.

Based on my understanding, now the problem is: You use simple binding to
bind a object to a text box. However, if you change the value
programmatically, the corresponding textbox won''t show the new data. Is it
right?

I started from our WinForm quick start sample at this directory if you
installed .NET framework samples:
C:\Program Files\Microsoft Visual Studio
NET\FrameworkSDK\Samples\QuickStart\winforms\sampl es\data\simplebinding\cs

In this project, I added a button to the form and in the OnClick handler of that button, I added codes:
custList[this.BindingContext[custList].Position+1].FirstName = "Yanhong";

Under this situation, the data of textbox won''t be refreshed. Only after
you click next, and then go back, the new value can be shown. In order to
let TextBox to show the newest data, we could add the following code:
textBoxFirstName.Text =
custList[this.BindingContext[custList].Position+1].FirstName;

Besides, in the code, we can set the text field of textbox to the new value first and then update it to the upderlying binding source.
[C#]
this.textBox1.Text = "XXXX"; //set the value
this.textBox1.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
//end the edit

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.






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

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