简单的数据绑定 - 如何处理绑定字段/属性的改变。的WinForms,净 [英] Simple databinding - How to handle bound field/property change. Winforms, .Net

查看:108
本文介绍了简单的数据绑定 - 如何处理绑定字段/属性的改变。的WinForms,净的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定属性的自定义控制: -

I have a custom control with a bindable property:-

Private _Value As Object
<Bindable(True), ... > _
Public Property Value() As Object
    Get
        Return _Value
    End Get
    Set(ByVal value As Object)
        _Value = value
    End Set
End Property

任何时候领域,即价值必然会变化,我需要的类型。

Any time the field, that Value is bound to, changes, I need to get the type.

我做这两个地方。首先在OnBindingContextChanged: -

I do this at two places. Firstly at OnBindingContextChanged:-

Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
    MyBase.OnBindingContextChanged(e)
    RemoveHandler Me.DataBindings.CollectionChanged, AddressOf DataBindings_CollectionChanged
    AddHandler Me.DataBindings.CollectionChanged, AddressOf DataBindings_CollectionChanged
    Me.MyBinding = Me.DataBindings("Value")
    If Me.MyBinding IsNot Nothing Then
        Me.GetValueType(Me.MyBinding)
    End If
End Sub

另外,在这里,我加入一个处理程序的DataBindings.CollectionChanged事件。 这是我检索类型第二位: -

Also, here, I'm adding a handler to the DataBindings.CollectionChanged event. This is the second place that I retrieve the type:-

Private Sub DataBindings_CollectionChanged(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)

    If e.Action = CollectionChangeAction.Add Then

        Dim b As Binding = DirectCast(e.Element, Binding)
        If b.PropertyName = "Value" Then
            Me.GetValueType(b)
        End If

    End If
End Sub

我需要放在第一位,因为BindingContextChanged事件不会触发之前的InitializeComponent后的一段时间。 所需的第二个地方,如果绑定字段编程改变。

I need the first place, because the BindingContextChanged event is not fired until some time after InitializeComponent. The second place is needed if the binding field is programatically changed.

我在这里处理正确的事件,或者是有一个更清洁的方式来做到这一点?

Am I handling the correct events here, or is there a cleaner way to do it?

请注意:我GetValueType方法使用CurrencyManager.GetItemProperties ....等,检索类型

Note: My GetValueType method uses the CurrencyManager.GetItemProperties....etc, to retrieve the type.

干杯,

ETA:刚才在这里清楚,我想知道当绑定字段发生了变化,而不是绑定字段值

ETA: Just to be clear here, I want to know when the bound field has changed, not the bound field value.

推荐答案

这听起来像你正在寻找的INotifyPropertyChange接口,更新将自动通知绑定控件。

It sounds like you are looking for the INotifyPropertyChange interface, which will automatically notify bound controls of an update.

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

这篇关于简单的数据绑定 - 如何处理绑定字段/属性的改变。的WinForms,净的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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