0

我有一个测验应用程序,其中最多可以有 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 的新手,不知道所有这些额外的控件......我现在已经将我的按钮转换为 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

不幸的是,按钮颜色没有改变。知道为什么吗?

4

1 回答 1

3

简而言之,你做错了。您需要识别行为(处于两种状态之一的按钮)并将其与该行为的视觉表现区分开来。这样做会导致您使用 aToggleButton并根据其IsChecked属性简单地更改其外观。

于 2012-04-14T08:44:10.493 回答