将复选框绑定到自定义类中的共享属性

时间:2019-07-07 16:26:39

标签: vb.net data-binding

所以我想这应该很简单,但是我从未做到过。

拿到一个我有一个复选框的表格。我希望此复选框直接链接到自定义类的Boolean属性,但是以某种方式无法正常工作。

Application.ActiveExplorer.Selection(1)

以上操作无效。它抱怨“ someClass”是一个类。

我也尝试过:

Public Class someClass
    Private Shared _Filter_Neighbor_6X1 As Boolean = True
    Shared Property Filter_Neighbor_6X1() As Boolean
        Get
            Return _Filter_Neighbor_6X1
        End Get
        Set(ByVal Value As Boolean)
            _Filter_Neighbor_6X1 = Value
        End Set
    End Property
End Class

Public Class GameGUI
    Private Sub GameGUI_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    chk_FilterAll.DataBindings.Add("Checked", someClass, "Filter_Neighbor_6X1")
    end sub
End Class

在启动时将应用挂起。

感谢您的帮助。

谢谢!

2 个答案:

答案 0 :(得分:0)

您无法绑定到共享属性。您需要从属性中删除Shared修饰符,创建类的实例,然后将该实例用于数据绑定。您的课程如下所示:

Public Class someClass
    Private _Filter_Neighbor_6X1 As Boolean = True
    Public Property Filter_Neighbor_6X1() As Boolean
        Get
            Return _Filter_Neighbor_6X1
        End Get
        Set(ByVal Value As Boolean)
            _Filter_Neighbor_6X1 = Value
        End Set
    End Property
End Class

然后您可以执行以下操作:

Dim instanceOfSomeClass As New someClass()
chk_FilterAll.DataBindings.Add("Checked", instanceOfSomeClass, "Filter_Neighbor_6X1")

请注意,如果在属性的GetSet下没有多余的逻辑,则可以摆脱后备字段并将其转换为自动实现(简写)属性: *

Public Property Filter_Neighbor_6X1 As Boolean = true

此外,如果您不想每次都创建该类的实例,则可以make it a Singleton

Public Class someClass
    Private Shared _instance As someClass

    Private Sub New()
    End Sub

    Public Shared ReadOnly Property Instance As someClass
        Get
            If _instance Is Nothing Then _instance = New someClass()
            Return _instance
        End Get
    End Property

    Public Property Filter_Neighbor_6X1 As Boolean = True
End Class

然后,您可以像这样直接使用它:

chk_FilterAll.DataBindings.Add("Checked", someClass.Instance, "Filter_Neighbor_6X1")

* 在这种特殊情况下,您可能要做需要额外的逻辑,以便通知属性更改,以便控件可以反映该更改。查看other answer了解更多信息。

答案 1 :(得分:0)

如果您不想摆脱Shared修饰符,则可以添加一个只读属性,该属性返回Shared的值:

Public Class someClass
    ' ...
    ' ...

    ' TODO: Remember to use a different name.
    Public ReadOnly Property MyNonSharedProperty As Boolean
        Get
            Return Filter_Neighbor_6X1
        End Get
    End Property   
End Class

然后您可以像这样使用它:

Dim instanceOfSomeClass As New someClass()
chk_FilterAll.DataBindings.Add("Checked", instanceOfSomeClass, "MyNonSharedProperty")

或采用Singleton方法(有关详细信息,请检查other answer

chk_FilterAll.DataBindings.Add("Checked", someClass.Instance, "MyNonSharedProperty")

但是,请注意,如果您要在课程 * 中实现INotifyPropertyChanged接口,则需要通知读取内容的更改专有属性以及共享属性更改时的共享属性。

完整实施:

Public Class someClass
    Implements INotifyPropertyChanged

    ' ##################################
    ' Optional: Add Singleton logic here
    ' ##################################

    Private _Filter_Neighbor_6X1 As Boolean = True
    Public Property Filter_Neighbor_6X1() As Boolean
        Get
            Return _Filter_Neighbor_6X1
        End Get
        Set(ByVal Value As Boolean)
            _Filter_Neighbor_6X1 = Value
            NotifyPropertyChanged()
            ' TODO: Remember to change the name of the property.
            NotifyPropertyChanged("MyNonSharedProperty")
        End Set
    End Property

    ' TODO: Remember to use a different name.
    Public ReadOnly Property MyNonSharedProperty As Boolean
        Get
            Return Filter_Neighbor_6X1
        End Get
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

* 将类用作数据绑定源时应该做的事情。