用户控制和绑定

时间:2010-03-11 02:14:50

标签: vb.net winforms data-binding

我创建了一个自定义控件,它由两个单选按钮组成,其外观设置为“Button”。此控件的作用类似于两个切换按钮,其中包含“开”切换按钮和“关闭”切换按钮。我有一个公共布尔属性“isAOn”,用于设置两个按钮的状态,我希望能够将此属性绑定到布尔值。我导入了组件模型并设置了

现在,当我将其添加到我的一个类中的布尔值的表单时,当我更改按下的按钮时,它似乎不会更新类的布尔值。

关于如何解决这个问题以及对课堂设计的建设性批评的建议非常受欢迎。

谢谢!

以下是代码:

Imports System.ComponentModel

''#
<DefaultBindingProperty("isAOn")> _
Public Class ToggleButtons

    Private _isAOn As Boolean
    Private _aText As String
    Private _bText As String
    Private _aOnColor As Color
    Private _aOffColor As Color
    Private _bOnColor As Color
    Private _bOffColor As Color

    Public Sub New()

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

        ''# Add any initialization after the InitializeComponent() call.
        aOffColor = Color.LightGreen
        aOnColor = Color.DarkGreen
        bOffColor = Color.FromArgb(255, 192, 192)
        bOnColor = Color.Maroon
        isAOn = False
        aText = "A"
        bText = "B"
    End Sub

    Private Sub configButtons()
        If _isAOn Then
            rbA.Checked = True
            rbA.BackColor = _aOnColor
            rbB.Checked = False
            rbB.BackColor = _bOffColor
        Else
            rbA.Checked = False
            rbA.BackColor = _aOffColor
            rbB.Checked = True
            rbB.BackColor = _bOnColor
        End If
        rbA.Text = aText
        rbB.Text = bText
    End Sub

    Public Property isAOn() As Boolean
        Get
            Return _isAOn
        End Get
        Set(ByVal value As Boolean)
            _isAOn = value
            configButtons()

        End Set
    End Property

    Private Sub rbOn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbA.CheckedChanged
        isAOn = rbA.Checked
    End Sub

    Private Sub rbOff_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbB.CheckedChanged
        isAOn = Not rbB.Checked
    End Sub

    Public Property aText() As String
        Get
            Return _aText
        End Get
        Set(ByVal value As String)
            _aText = value
        End Set
    End Property
    Public Property bText() As String
        Get
            Return _bText
        End Get
        Set(ByVal value As String)
            _bText = value
        End Set
    End Property
    Public Property aOnColor() As Color
        Get
            Return _aOnColor
        End Get
        Set(ByVal value As Color)
            _aOnColor = value
        End Set
    End Property
    Public Property aOffColor() As Color
        Get
            Return _aOffColor
        End Get
        Set(ByVal value As Color)
            _aOffColor = value
        End Set
    End Property
    Public Property bOnColor() As Color
        Get
            Return _bOnColor
        End Get
        Set(ByVal value As Color)
            _bOnColor = value
        End Set
    End Property
    Public Property bOffColor() As Color
        Get
            Return _bOffColor
        End Get
        Set(ByVal value As Color)
            _bOffColor = value
        End Set
    End Property

End Class

1 个答案:

答案 0 :(得分:0)

您需要添加isAOnChanged事件并在属性设置器中将其提升。

顺便说一下,.Net中的属性和方法应该是UpperCamelCased。