1
我创建了一个自定义控件,它由两个单选按钮组成,其外观设置为“按钮”。这个控件的作用就像两个切换按钮,带有“开”切换按钮和“关”切换按钮。我有一个公共布尔属性“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
谢谢,但我认为还需要做更多的事情,而不仅仅是添加事件并提升它。 应该如何处理? 有没有办法自动保持这种双向绑定? – ChrisAU 2010-03-11 02:28:02
不需要。您需要做的就是创建一个正确名称和类型的事件('EventHandler'),并在值发生更改时触发它。 – SLaks 2010-03-11 02:29:32
SLaks,我添加了一个事件“Public Event isAOnChanged As EventHandler”,并在属性设置器中引发了这个事件,但对象中绑定的布尔属性仍然没有得到任何活动。 – ChrisAU 2010-03-11 02:36:48