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 andcalling EndCurrentEdit discards the new value. Changing a custom propertyand calling EndCurrentEdit accepts the new value, stores it in the datasoureand behaves normally. Here is a reproduceable example: First, extend Textbox: Public Class MyTextBoxInherits TextBox Private myProp As String Public Property MyProperty() As StringGetReturn myPropEnd GetSet(ByVal Value As String)myProp = ValueEnd SetEnd PropertyEnd Class Then, use this new Textbox on a form, and run this example. You will notice, that calling just TestCustomProperty, updates the value inthe datasource and in what is displayed in the textbox.Calling just TestTextProperty discards the value and does not updateanything. What I want to understand is - why the behavior difference? Note, I have experimented, and binding to the Text properties of 2 differenttextboxes 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 verystrangely when the column it is bound to, is also bound to another non-Textproperty. Here is the Form code: Public Class Form2Inherits 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 ThenIf Not (components Is Nothing) Thencomponents.Dispose()End IfEnd IfMyBase.Dispose(disposing)End Sub ''Required by the Windows Form DesignerPrivate 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 SubInitializeComponent()Me.MyTextBox1 = New VisionControlTester.MyTextBoxMe.SuspendLayout()''''MyTextBox1''Me.MyTextBox1.Location = New System.Drawing.Point(80, 72)Me.MyTextBox1.MyProperty = NothingMe.MyTextBox1.Name = "MyTextBox1"Me.MyTextBox1.TabIndex = 0Me.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 RegionDim dt As New DataTable Private Sub Form2_Load(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles MyBase.Loaddt.Columns.Add("MyColumn")Dim row As DataRow = dt.NewRowrow(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 SubEnd Class推荐答案 嗨Marina, 最好留在你知道的同一个帖子中,但是请看我的消息在 的起源al thread。 Cor Hi Marina, Better is to stay in the same thread as you know, however see my message inthe 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, theywould 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 ofthem for their Text property, and used just the datatable - all would workgreat. It only starts breaking when Text and non-Text properties are mixed - and Iwant to know why. If it''s 2 Text properties, everything is well. And if itis a Text property and some other property - then changing the non-Textproperty works as expected - it is only when doing the same with a Textproperty 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 wouldallow people to have controls on a form bound to any column - possibly somecolumns being bound to by more then one control. If I need to start creating a separate dataview for each time I add adatabinding, I can only imagine the performance hit, not to mention themanagement issue."Cor Ligthert" <no**********@planet.nl> wrote in messagenews:ub**************@TK2MSFTNGP09.phx.gbl...嗨Marina, 最好留在你知道的同一个帖子中,但是在原始帖子中看到我的消息。 Cor Hi Marina, Better is to stay in the same thread as you know, however see my messagein 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 thelogic 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 wantto use the text part real as a textbox. Cor \\\Dim dt As New DataTablePrivate Sub Form2_Load(ByVal sender As Object, _ByVal e As System.EventArgs) Handles MyBase.Loaddt.Columns.Add("MyColumn")Dim row As DataRow = dt.NewRowrow(0) = "TestValue"dt.Rows.Add(row)MyTextBox1.DataBindings.Add("MyProperty", dt, "MyColumn")MyTextBox1.DataBindings.Add("Text", dt, "MyColumn")End SubPrivate Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickMyTextBox1.Text = "TextValue"Me.BindingContext(dt).EndCurrentEdit()End SubPrivate Sub Button2_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button2.ClickMyTextBox1.MyProperty = "PropValue"Me.BindingContext(dt).EndCurrentEdit()End SubEnd Class/// 这篇关于重新绑定问题 - 数据绑定中的不一致行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 23:03