我有一个测验应用程序,最多可以有3个答案。答案是已分配样式的按钮。所有答案按钮都有相同的点击事件...
Private Sub butAnswer_Click(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
'An Answer has been clicked
Dim butC as Button
Dim butS as String = TryCast(sender, Button).Name.ToString()
'Cycle through all answers and set style
For x = 1 to 3
butC = DirectCast(FindName("butAnswer" & x), Button)
If butS = "butAnswer" & x.ToString Then
butC.Style = DirectCast(FindResource("GlassButtonSelected"), Style)
Else
butC.Style = DirectCast(FindResource("GlassButton"), Style)
End If
Next
End Sub
我现在想要对此进行扩展,以便如果用户第二次点击“选定”答案,则样式将恢复为“GlassButton”
If butS = "butAnswer" & x.ToString Then
'Check what style the button has
If 'style is GlassButton' Then
butC.Style = DirectCast(FindResource("GlassButtonSelected"), Style)
Else
butC.Style = DirectCast(FindResource("GlassButton"), Style)
End If
Else
butC.Style = DirectCast(FindResource("GlassButton"), Style)
End If
我的问题是如何确定按钮的样式?
额外更新
肯特, 谢谢,我是wpf的新手,并不知道所有这些额外的控件...... 我现在已经将我的Buttons转换为ToggleButtons并且有一个由所有ToggleButtons使用的已检查和未检查的事件。我修改了我的风格,目标是ToggleButton,我现在只有一个。我已经摆脱了用来表示所选答案的风格 - 基本上是背景颜色的变化。我的Checked活动有......
Private Sub togAnswer_Checked(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
'togAnswer was clicked....
Dim togC as ToggleButton
Dim togS as String = TryCast(sender, ToggleButton).Name.ToString()
'Cycle through all answers and set style
Dim bc = New BrushConverter()
For x = 1 to 3
togC = DirectCast(FindName("togAnswer" & x), ToggleButton)
If togS = "togAnswer" & x.ToString Then
'Set ToggleButton to selected colour
togC.Background = DirectCast(bc.ConvertFrom("#AAFF8020"), Brush)
Else
'Set other ToggleButtons to unselected colour
togC.Background = DirectCast(bc.ConvertFrom("#AA000000"), Brush)
End If
Next
End Sub
不幸的是,按钮颜色不会改变。知道为什么吗?
答案 0 :(得分:3)
简而言之,你做错了。您需要识别行为(处于两种状态之一的按钮)并将其与该行为的视觉表现区分开来。这样做会导致您使用ToggleButton
并根据其IsChecked
属性更改其外观。