数据源绑定到用户属性时更新 [英] datasource Updating when bind to user property

查看:125
本文介绍了数据源绑定到用户属性时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,VS-2010,winforms

通常,我想使用可绑定的MyProperty创建用户控件.但是我遇到了一个问题:当当前行更改时,数据行将被修改.

您可以下载C#项目并自己尝试:
test_bind.zip



为了简单起见,我做了以下操作:
1.使用
创建测试表单 * dataGridView(将其设置为ReadOnly),
*文本框,
*一些类型化的DataSet和一些Table
* bindingSource
2.将bindingSource绑定到表
3.将dgv绑定到bindingSource
4.将TextBox.Text绑定到bindingSource到某个文本列(名称")
5.在表格加载表中填入一些数据. (如果您手动添加行,请不要忘记AcceptChanges())
6.为表xxx_ColumnChanging(...)添加事件处理程序,并在那里设置断点

所有标准步骤,没什么特别的

运行

符合预期-dgv中的行,textBox中的当前名称,单击网格中的其他行...并且仅当我修改textbox中的文本时,我才会在断点处停止.

让我们修改程序,并将textBox绑定到不同的属性而不是Text 上-让我们说 Tag .将DataSourceUpdateProperty设置为"OnValidation"或"OnPropertyChanged".

运行

现在,当前行被修改. !!!

当我使用text属性使用UserControl而不是textBox时,也会发生同样的情况
字符串MyProperty {设置;得到; }

如何以与TextBox.Text相同的行为绑定用户控件中的自定义属性?

注意:
绑定属性本身不是问题.问题是:当我绑定到标准TextBox.Text属性时-一切正常,我将绑定到用户控件中的任何其他属性(标记)或自定义属性-然后,数据源总是会被修改为当前行

C#, VS-2010, winforms

Generally I want to create user control with a bindable MyProperty. But I met some problem: Data row become modified when current row changed.

You can download C# project and try it yourself:
test_bind.zip



To make it simple I did follow:
1. create test form with a
* dataGridView (set it ReadOnly),
* TextBox,
* some typed DataSet with some Table
* bindingSource
2. bind bindingSource to the table
3. bind dgv to bindingSource
4. bind TextBox.Text to bindingSource to some text column ("name")
5. on form load fill table with some data. (If you add rows manually do not forget AcceptChanges())
6. add event handler for table xxx_ColumnChanging(...) and set breakpoint there

all standard steps, nothing special

Run

as expected - rows in dgv, current name in textBox, click on different rows in grid... and only when I modify text in textbox I stop in breakpoint.

Lets modify program, and bind textBox to different property instead of Text - lets say Tag. Set DataSourceUpdateProperty "OnValidation" or "OnPropertyChanged".

Run

Now current row become modified. !!!

Same happens when instead of textBox I use UserControl with a dummy property
String MyProperty { set; get; }

How to bind custom property in user control with the same behaviour as TextBox.Text?

Note:
Binding property itself is not a problem. Problem is: when I bind to standard TextBox.Text property - everything Ok Wen I bind to ANY OTHER property (Tag) or to custom Property in User Control - then DATASOURCE ALWAYS BECOME MODIFIED FOR CURRENT ROW

推荐答案

也许这可以帮助您:
http://victorhurdugaci.com/binding-in-winforms-like-in-wpf-1 / [ ^ ]
Perhaps this could help you:
http://victorhurdugaci.com/binding-in-winforms-like-in-wpf-1/[^]


简短的答案是:

绑定到简单属性时,声明为

Short answer is:

When bind to simple property, declared like

[Bindable(true, BindingDirection.TwoWay)]           //  I need TwoWay    
public String MyProp {set;get;}



然后数据源将根据当前位置变化进行更新.您可以将其视为错误或功能...

改变行为-我需要魔术.在控件中将事件声明为



then datasource will be updated on current position change. You may count it as Bug or Feature...

To change behavior - I need magic word. Declare event in the control as

public event System.ComponentModel.PropertyChangedEventHandler MyPropChanged;



此名称很特殊:"YourPropertyName" +"Changed" = YourPropertyNameChanged
换句话说,这不会是魔法.
声明事件后-数据源不会像以前一样每次都更新...

现在我可以控制流程了.



This name is special: "YourPropertyName" + "Changed" = YourPropertyNameChanged
Other word will not be magic.
As soon as event declared - datasource would not be updated every time as before...

and now I can control the process...

public void OnMyPropChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
    Binding bb = this.DataBindings[0] as Binding;
    bb.WriteValue();
    if (MyPropChanged != null) MyPropChanged(this, e);
}

private void TxtChanged(object sender, EventArgs e)
{
    if (load) { return; }
    _my_prop = Txt.Text;   // ...
    OnValueTwoChanged(new PropertyChangedEventArgs("MyProp"));
}



这是具有更多详细信息的示例项目,其中我使用String属性 ValueTwo
创建用户控件 使用一些自定义逻辑将其拆分为两个不同的TextBox,并在用户更改文本时组合值和更新.


test_bind_fixed.zip



Here is the example project with more details, where I create user control with String property ValueTwo
which split in two different TextBoxes using some custom logic, and combine value and update when user change the text.


test_bind_fixed.zip


这篇关于数据源绑定到用户属性时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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