ListBox control in WPF
ListBox:
Namespace : System.Windows.Controls.ListBox
Designer Code:
<ListBox SelectionChanged="ListBox_SelectionChanged" Name="lst1">
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
</ListBox>
Code Behind:
// Window1.xaml.cs
private void ListBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
ListBoxItem litems = (ListBoxItem)lst1.SelectedItem;
}
adding Items from code:
listBox1.Items.Add("one");
listBox1.Items.Add("two");
listBox1.Items.Add("three");
Removing Items from Listbox:
listBox1.Items.RemoveAt
(listBox1.Items.IndexOf(listBox1.SelectedItem));
This is a ItemsControl.
Because the items can display richer element trees, it is far easier to add images to your items in your listbox, where as in the old ListBox you had to do OwnerDraw.
There are several properties of major interest to the ListBox:
Items – specifies the items to display in the listbox.
ItemsSource – specifies some sort of collection that will represent the contents of the listbox (aka databinding)
ItemsTemplate – specifies a DataTemplate that can be used to generate the UI for the items in the ItemsSource. (e.g. for every Person in the People collection generate an image, a text block for the first name, a text block for the last name).
ItemsPanel – specifies a way to create an alternate layout for the listbox – instead of having the items appear one after the other
Note that you can either use Items OR use ItemsSource, using Items directly trumps the databinding. (Note stuff in the middle of the <ListBox> XAML tag populates the Items collection [3] – so this would break databinding.)
Images in a ListBox:
<ListBox SelectionChanged="ListBox_SelectionChanged" Name="lst1"
Margin="0,0,12,12">
<ListBoxItem Background="LightBlue" Foreground="Yellow"
FontFamily="Verdana" FontSize="12" FontWeight="Bold">
<StackPanel Orientation="Horizontal">
<Image Source="Images\win2wpf.JPG" Height="30"></Image>
<TextBlock Text="1"></TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem Background="LightBlue" Foreground="Black"
FontFamily="Verdana" FontSize="12" FontWeight="Bold">
<StackPanel Orientation="Horizontal">
<Image Source="Images\win2wpf.JPG" Height="30"></Image>
<TextBlock Text="2"></TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem Background="LightBlue" Foreground="Red"
FontFamily="Verdana" FontSize="12" FontWeight="Bold">
<StackPanel Orientation="Horizontal">
<Image Source="Images\win2wpf.JPG" Height="30"></Image>
<TextBlock Text="3"></TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
<ListBox ItemsSource="{Binding dataObj}" Height="138" Width="310">
<ListBoxItem></ListBoxItem>
</ListBox>
Binding an array to listbox:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPF1.MyPage"
Loaded="MyPage_Loaded">
<ListBox Name="listBox1">
</ListBox>
</Window>
Code behind:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WPFApp
{
public partial class Window1 : System.Windows.Controls.Window
{
public Window1()
{
InitializeComponent();
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
string[] lst = { "One", "Two", "Three" };
listBox1.ItemsSource = lst;
}
}
}
Binding object to listbox:
<Window.Resources>
<ObjectDataProvider x:Key="data" ObjectType="{x:Type l:Names}"/>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Source={StaticResource dataobject}}"/>
Code behind:
public class Names : List<string>
{
public Names()
{
this.Add("One");
this.Add("Two");
this.Add("Three");
}
}
Binding Database to listbox:
private void BindData()
{
DataSet dtSet = new DataSet();
using (connection = new SqlConnection(connectionString))
{
command = new SqlCommand(sql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
connection.Open();
adapter.SelectCommand = command;
adapter.Fill(dtSet, "Customers");
listBox1.DataContext = dtSet;
}
}
Binding XML to listbox:
<ListBox Width="400" Height="300" Background="LightGray">
<ListBox.ItemsSource>
<Binding Source="{StaticResource XmldataSourcetobind}"
XPath="*[@Type='variable'] "/>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock Foreground="Green" >
<TextBlock.Text>
<Binding XPath="variable"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ListBox with DataTemplate:
<ListBox Grid.Row="0" x:Name="lst1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Names}" />
<Button Command="{Binding Path=IdText}" >Button</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>