重新绑定问题 - 数据绑定中的不一致行为 [英] Binding problem restated - inconsistent behavior in databinding

查看:104
本文介绍了重新绑定问题 - 数据绑定中的不一致行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在。如果相同(或不同)

控件上的2个不同属性绑定到同一数据列,则更改Text属性和调用EndCurrentEdit的
会丢弃新值。更改自定义属性

并调用EndCurrentEdit接受新值,将其存储在datasoure

中并且行为正常。这是一个可重现的例子:


首先,扩展文本框:


公共类MyTextBox

继承TextBox


私有myProp字符串


公共属性MyProperty()字符串

获取

返回myProp

结束获取

设置(ByVal值为字符串)

myProp = Value

结束集

结束属性

结束类


然后,在表单上使用这个新的文本框,并运行此示例。


您会注意到,只调用TestCustomProperty,更新数据源中的值以及文本框中显示的内容。

只调用TestTextProperty丢弃价值并且不更新

任何东西。


我想了解的是 - 为什么行为不同?


注意,我已经尝试过,绑定到2个不同的文本属性

文本框工作正常。

这只是绑定到Text属性和另一个属性的问题。

有一些关于Text属性的东西似乎使它表现得很好

奇怪的是当它绑定到的列时,也是另一个非文本

属性。


这是表格代码:


公开Class Form2

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为布尔)

如果处置那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

结束子


'' Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:Windows窗体设计器需要以下过程

''可以使用Windows窗体设计器修改它。

''不要莫使用代码编辑器来区分它。

Friend WithEvents MyTextBox1 As VisionControlTester.MyTextBox

< System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

Me.MyTextBox1 =新VisionControlTester.MyTextBox

Me.SuspendLayout()

''

''MyTextBox1

''

Me.MyTextBox1.Location = New System.Drawing.Point(80,72)

Me.MyTextBox1.MyProperty = Nothing

Me.MyTextBox1.Name =" MyTextBox1"

Me.MyTextBox1.TabIndex = 0

Me.MyTextBox1.Text =" MyTextBox1"

''

''Form2

''

Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)

Me.ClientSize = New System.Drawing.Size(292,266)

Me .Controls.Add(Me.MyTextBox1)

Me.Name =" Form2"

Me.Text =" Form2"

Me .ResumeLayout(False)


End Sub


#End Region

Dim dt As New DataTable


Private Sub Form2_Load(ByVal sender As Object,ByVal e As

System.EventArgs)Ha ndles MyBase.Load

dt.Columns.Add(" MyColumn")

Dim row As DataRow = dt.NewRow

row(0 )=" TestValue"

dt.Rows.Add(row)

MyTextBox1.DataBindings.Add(" Text",dt," MyColumn" ;)

MyTextBox1.DataBindings.Add(" MyProperty",dt," MyColumn")

TestTextProperty()

TestCustomProperty()

End Sub


Private Sub TestTextProperty()

MyTextBox1.Text =" NewValue" ;

Me.BindingContext(dt).EndCurrentEdit()

结束子


私有子TestCustomProperty()

MyTextBox1.MyProperty =" NewValue"

Me.BindingContext(dt).EndCurrentEdit()

End Sub

End Class

Here is the problem. If 2 different properties on the same (or different)
control are bound to the same data column, changing the Text property and
calling EndCurrentEdit discards the new value. Changing a custom property
and calling EndCurrentEdit accepts the new value, stores it in the datasoure
and behaves normally. Here is a reproduceable example:

First, extend Textbox:

Public Class MyTextBox
Inherits TextBox

Private myProp As String

Public Property MyProperty() As String
Get
Return myProp
End Get
Set(ByVal Value As String)
myProp = Value
End Set
End Property
End Class

Then, use this new Textbox on a form, and run this example.

You will notice, that calling just TestCustomProperty, updates the value in
the datasource and in what is displayed in the textbox.
Calling just TestTextProperty discards the value and does not update
anything.

What I want to understand is - why the behavior difference?

Note, I have experimented, and binding to the Text properties of 2 different
textboxes works fine.
This is only a problem in binding to the Text property and another property.
There is something about the Text property that seems to make it behave very
strangely when the column it is bound to, is also bound to another non-Text
property.

Here is the Form code:

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
Friend WithEvents MyTextBox1 As VisionControlTester.MyTextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.MyTextBox1 = New VisionControlTester.MyTextBox
Me.SuspendLayout()
''
''MyTextBox1
''
Me.MyTextBox1.Location = New System.Drawing.Point(80, 72)
Me.MyTextBox1.MyProperty = Nothing
Me.MyTextBox1.Name = "MyTextBox1"
Me.MyTextBox1.TabIndex = 0
Me.MyTextBox1.Text = "MyTextBox1"
''
''Form2
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.MyTextBox1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
Dim dt As New DataTable

Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dt.Columns.Add("MyColumn")
Dim row As DataRow = dt.NewRow
row(0) = "TestValue"
dt.Rows.Add(row)

MyTextBox1.DataBindings.Add("Text", dt, "MyColumn")
MyTextBox1.DataBindings.Add("MyProperty", dt, "MyColumn")

TestTextProperty()

TestCustomProperty()
End Sub

Private Sub TestTextProperty()
MyTextBox1.Text = "NewValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub

Private Sub TestCustomProperty()
MyTextBox1.MyProperty = "NewValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
End Class

推荐答案

嗨Marina,


最好留在你知道的同一个帖子中,但是请看我的消息在

的起源al thread。


Cor
Hi Marina,

Better is to stay in the same thread as you know, however see my message in
the original thread.

Cor


我发布了一个新主题,如果人们对旧主题失去兴趣,他们

会看到重述的版本。


你是对的,你的例子确实有效。


问题是:为什么?


如果我做了同样的事情,除了有2个文本框之外,为每个

添加了一个绑定它们的Text属性,并且只使用了数据表 - 一切都会工作

很棒。


它只在文本和非文本属性混合时开始破坏 - 我

想知道原因。如果它是2个文本属性,一切都很好。如果它是
是Text属性和其他一些属性 - 那么更改非Text

属性就会按预期工作 - 只有在使用Text

它破坏的属性。


那么为什么单向工作而不是另一种工作(如我原来的例子)?


问题在于我正在开发一些非常通用的东西,这将允许人们对绑定到任何列的表单进行控制 - 可能是一些

由多个控件绑定的列。


如果我每次添加一个

数据绑定时需要开始创建单独的数据视图,我只能想象性能受到打击,更不用说

管理问题了。

" Cor Ligthert" <无********** @ planet.nl>在消息中写道

news:ub ************** @ TK2MSFTNGP09.phx.gbl ...
I posted a new thread, so that if people lost interest in the old one, they
would see the restated version.

You are right, your example does work.

The question is: why?

If I did the same thing, except had 2 textboxes, added a binding for each of
them for their Text property, and used just the datatable - all would work
great.

It only starts breaking when Text and non-Text properties are mixed - and I
want to know why. If it''s 2 Text properties, everything is well. And if it
is a Text property and some other property - then changing the non-Text
property works as expected - it is only when doing the same with a Text
property that it breaks.

So why work one way and not the other (like in my original example)?

The issue being is that I''m working on something very generic that would
allow people to have controls on a form bound to any column - possibly some
columns being bound to by more then one control.

If I need to start creating a separate dataview for each time I add a
databinding, I can only imagine the performance hit, not to mention the
management issue.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
嗨Marina,

最好留在你知道的同一个帖子中,但是在原始帖子中看到我的消息


Cor
Hi Marina,

Better is to stay in the same thread as you know, however see my message in the original thread.

Cor



嗨Marina,


这个你不相信,我在想也许有什么东西在

逻辑,它保持绑定的值并使用它。


测试我将代码更改为此。


和你可能会告诉我这里发生了什么,但我认为你可以使用它。


对我而言,它与我想要的组合框中的文字相同

使用文本部分真实作为文本框。


Cor


\\\

Dim dt As New DataTable

Private Sub Form2_Load(ByVal sender As Object,_

ByVal e As System.EventArgs)处理MyBase.Load

dt.Colu mns.Add(" MyColumn")

Dim row As DataRow = dt.NewRow

row(0)=" TestValue"

dt.Rows.Add(row)

MyTextBox1.DataBindings.Add(" MyProperty",dt," MyColumn")

MyTextBox1.DataBindings.Add("文字",dt," MyColumn")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object,_

ByVal e作为System.EventArgs)处理Button1.Click

MyTextBox1.Text =" TextValue"

Me.BindingContext(dt).EndCurrentEdit()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object,_

ByVal e As System.EventArgs)处理Button2.Click

MyTextBox1.MyProperty =" PropValue"

Me.BindingContext(dt).EndCurrentEdit()

End Sub

End Class

///
Hi Marina,

This you do not believe, I was thinking maybe is there something in the
logic that it keeps the value from the binding and uses that.

To test that I changed the code to this.

And you may tell me what happens here, however I think you can use it.

For me it is something the same as that text from the combobox where I want
to use the text part real as a textbox.

Cor

\\\
Dim dt As New DataTable
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("MyColumn")
Dim row As DataRow = dt.NewRow
row(0) = "TestValue"
dt.Rows.Add(row)
MyTextBox1.DataBindings.Add("MyProperty", dt, "MyColumn")
MyTextBox1.DataBindings.Add("Text", dt, "MyColumn")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MyTextBox1.Text = "TextValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
MyTextBox1.MyProperty = "PropValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
End Class
///


这篇关于重新绑定问题 - 数据绑定中的不一致行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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