WPF - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WPF ListBox

WPF ListBox

shape Description

List box by default allows single selection only, but can be changed to multi-selection by setting the SelectionMode property as an Extended or Single or Multiple.

shape Example

Create a new window, then go to xmal source and write the following code: [csharp] <Window x:Class="WPFListbox.ListBox1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ListBox1" Height="300" Width="350" Background="Black"> <Grid> <ListBox Margin="63,12,65,9" Width="150" Height="240" Background="LightPink"> <ListBoxItem>January</ListBoxItem> <ListBoxItem>February</ListBoxItem> <ListBoxItem>March</ListBoxItem> <ListBoxItem>April</ListBoxItem> <ListBoxItem>May</ListBoxItem> <ListBoxItem>June</ListBoxItem> <ListBoxItem>July</ListBoxItem> <ListBoxItem>August</ListBoxItem> <ListBoxItem>September</ListBoxItem> <ListBoxItem>October</ListBoxItem> <ListBoxItem>November</ListBoxItem> <ListBoxItem>December</ListBoxItem> </ListBox> </Grid> </Window> [/csharp] Now run the application,a new window appears like below screen:

shape Example

Data binding with Item template for List box:Create a new window, then go to XAML source and write the following code: [csharp] <Window x:Class="WPFListbox.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | ListBox" Height="300" Width="300" Background="Black"> <Grid Margin="10"> <ListBox Name="lstempinfo" HorizontalContentAlignment="Stretch" Width="200" Margin="0,10,0,10" Background="LightPink" FontFamily="Tahoma" FontWeight="Bold"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding EmpName}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> [/csharp] Then the window becomes like below: Now go to code behind file and write the following code: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WPFListbox { /// <summary> /// Interaction logic for ListBox1.xaml /// </summary> public partial class ListBox1 : Window { public ListBox1() { InitializeComponent(); lstempinfo.ItemsSource = lstemp; GetEmp(); } List&amp;lt;EmployeeInfo&amp;gt; lstemp = new List&amp;lt;EmployeeInfo&amp;gt;(); void GetEmp() { lstemp.Add(new EmployeeInfo() { Empid = 111, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 112, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 113, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 114, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 115, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 116, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 117, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 111, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 112, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 113, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 114, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 115, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 116, EmpName = "SPlessons" }); lstemp.Add(new EmployeeInfo() { Empid = 117, EmpName = "SPlessons" }); } public class EmployeeInfo { public int Empid { get; set; } public string EmpName { get; set; } } } }[/csharp] Now run the application, the result appears like below.

shape Example

Data Binding from Database to List box:Create a new window, then go to xaml source and write the following code: [csharp] <Window x:Class="WPFListbox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | ListBox" Height="350" Width="325" Background="RosyBrown"> <Grid Margin="10"> <ListBox Name="emptrancations" HorizontalContentAlignment="Stretch" Width="200" Margin="0,10,0,10" Background="Maroon" FontWeight="Bold"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding EName}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> [/csharp] Then the window should become like below. Now go to code behind file and write the following code: [csharp]using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data.SqlClient; using System.Data; namespace WPFListbox { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); GetEmployee(); } void GetEmployee() { SqlConnection con = new SqlConnection("Server=srinu;database=Test;uid=sa;pwd=Srinu20!5;"); SqlCommand cmd = new SqlCommand("select * from Employee", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable ds = new DataTable(); da.Fill(ds); con.Open(); cmd.ExecuteReader(); con.Close(); emptrancations.ItemsSource = ds.DefaultView; } } } [/csharp] Now run the application, then result appears like below.