Posted by: Zeeshan Amjad | December 1, 2009

Using checkbox in list box


We can add the check box in list box just like we did with combo box. We have to define the data template for the listbox item. Here is a complete XAML code of this project.

  1: <Window x:Class="CheckBox.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Checked Box in ListBox" Height="300" Width="300">
  5:     <Grid>        
  6:         <ListBox Name="list" Margin="5" HorizontalContentAlignment="Stretch" >
  7:             <ListBox.ItemTemplate>
  8:                 <DataTemplate>
  9:                     <Border Margin="3" BorderBrush="Brown" BorderThickness="2" CornerRadius="3">
 10:                         <Border.Background>
 11:                             <LinearGradientBrush>
 12:                                 <GradientStop Offset="0" Color="Blue"/>
 13:                                 <GradientStop Offset="0.5" Color="AliceBlue"/>
 14:                                 <GradientStop Offset="1" Color="Navy"/>
 15:                             </LinearGradientBrush>
 16:                         </Border.Background>
 17:                         <StackPanel Orientation="Horizontal">
 18:                             <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding IsVisited}"/>
 19:                             <TextBlock Margin="2" Foreground="Yellow" FontSize="14"  Text="{Binding CityName}"/>
 20:                         </StackPanel>
 21:                     </Border>
 22:                 </DataTemplate>
 23:             </ListBox.ItemTemplate>
 24:         </ListBox>
 25:     </Grid>
 26: </Window>
 27: 

The C# code is same as previous project. Here is a complete C# coding of this project.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace CheckBox
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25: 
 26:             List<TripInfo> tripList = new List<TripInfo>();
 27: 
 28:             tripList.Add(new TripInfo(false, "Miami"));
 29:             tripList.Add(new TripInfo(true, "Boston"));
 30:             tripList.Add(new TripInfo(true, "Los Angeles"));
 31:             tripList.Add(new TripInfo(true, "Houston"));
 32:             tripList.Add(new TripInfo(false, "Dallas"));
 33:             tripList.Add(new TripInfo(false, "Atlantic City"));
 34:             tripList.Add(new TripInfo(true, "Chicago"));
 35: 
 36:             list.ItemsSource = tripList;
 37:         }
 38:     }
 39: 
 40:     public class TripInfo
 41:     {
 42:         public TripInfo(bool isVisited, string cityName)
 43:         {
 44:             IsVisited = isVisited;
 45:             CityName = cityName;
 46:         }
 47: 
 48:         public Boolean IsVisited
 49:         { get; set; }
 50: 
 51:         public String CityName
 52:         { get; set; }
 53:     }
 54: }
 55: 

Here is the output of this program.

CheckBoxListBox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: