WPF學習日誌 ~ RadioButton ~

RadioButton與之前在Windows Form的時候使用上沒有太大的差異,下面這邊我們特別說明一個GroupName這個屬性;在以往,同一個群組的項目我們會把它放到容器中,用容器來作分割;而在WPF裡面則是用GroupName來作分割,下面我們來看看測試的程式碼
Imports System.Windows
Imports System.IO
Imports System.Reflection

Class Window1
Dim spMain As StackPanel
Dim wpChild_1 As WrapPanel
Dim wpChild_2 As WrapPanel
Dim rdoA(2) As RadioButton
Dim rdoB(2) As RadioButton
Dim tbMsg As TextBlock

Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
spMain = New StackPanel
Me.Content = spMain
For z As Integer = 0 To 2
rdoA(z) = New RadioButton
rdoA(z).Content = "RadioButton-A-" & z
''不指定GroupName的話預設是""
'rdoA(z).GroupName = "A"
rdoA(z).Margin = New Thickness(3)
AddHandler rdoA(z).Checked, AddressOf RadioButton_Checked
rdoB(z) = New RadioButton
rdoB(z).Content = "RadioButton-B-" & z
rdoB(z).GroupName = "B"
rdoB(z).Margin = New Thickness(3)
AddHandler rdoB(z).Checked, AddressOf RadioButton_Checked
Next
wpChild_1 = New WrapPanel
wpChild_1.Background = Brushes.LightGreen
wpChild_1.Children.Add(rdoA(0))
wpChild_1.Children.Add(rdoA(1))
wpChild_1.Children.Add(rdoA(2))
wpChild_1.Children.Add(rdoB(0))

wpChild_2 = New WrapPanel
wpChild_2.Background = Brushes.LightPink
wpChild_2.Children.Add(rdoB(1))
wpChild_2.Children.Add(rdoB(2))

spMain.Children.Add(wpChild_1)
spMain.Children.Add(wpChild_2)

tbMsg = New TextBlock
spMain.Children.Add(tbMsg)
End Sub

Protected Sub RadioButton_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
If e.OriginalSource.GetType.Equals(GetType(RadioButton)) Then
tbMsg.Text = CType(e.OriginalSource, RadioButton).Content & "-->Checked"
End If
End Sub
End Class
如果不設定GruopName的時候行為就跟以往在Windows Form的時候是一樣的

留言

這個網誌中的熱門文章

開啟cshtml檔案時,出現『並未將物件參考設定為物件執行個體』的錯誤訊息

無法設定中斷點 尚未載入符號檔

DataGridView欄位計算總合