CheckBox List : ListBox « Windows Presentation Foundation « VB.Net Tutorial






<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
WPF Check Box List








16.27.ListBox
16.27.1.Bind String Array Resource to ListBoxBind String Array Resource to ListBox
16.27.2.Use Data Templates to Display Bound DataUse Data Templates to Display Bound Data
16.27.3.CheckBox ListCheckBox List
16.27.4.Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverterConvert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter
16.27.5.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.
16.27.6.Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverterConvert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
16.27.7.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
16.27.8.Add selected file to ListBoxAdd selected file to ListBox
16.27.9.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.
16.27.10.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
16.27.11.Ensure there is at least one item selectedEnsure there is at least one item selected