<Window x:Class="ClassicControls.CheckBoxList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckBoxList" Height="300" Width="300">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Name="lst" SelectionChanged="lst_SelectionChanged" CheckBox.Click="lst_SelectionChanged" >
<CheckBox Margin="3">Option 1</CheckBox>
<CheckBox Margin="3">Option 2</CheckBox>
<CheckBox Margin="3">Option 3</CheckBox>
</ListBox>
<StackPanel Grid.Row="1" Margin="0,10,0,0">
<Button Margin="0,10,0,0" Click="cmd_CheckAllItems">Examine All Items</Button>
</StackPanel>
</Grid>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Namespace ClassicControls
Public Partial Class CheckBoxList
Inherits System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub lst_SelectionChanged(sender As Object, e As RoutedEventArgs)
If TypeOf e.OriginalSource Is CheckBox Then
lst.SelectedItem = e.OriginalSource
End If
If lst.SelectedItem Is Nothing Then
Return
End If
Console.WriteLine(lst.SelectedIndex)
Console.WriteLine(DirectCast(lst.SelectedItem, CheckBox).IsChecked)
End Sub
Private Sub cmd_CheckAllItems(sender As Object, e As RoutedEventArgs)
For Each item As CheckBox In lst.Items
If item.IsChecked = True Then
Console.WriteLine((Convert.ToString(item.Content) & " is checked."))
End If
Next
End Sub
End Class
End Namespace
|