Without specifying a DataTemplate, the ListBox displays a list of names.


   
  

<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="300" Width="300">

    <Window.Resources>
        <WpfApplication1:People x:Key="people"/>
        
        <DataTemplate x:Key="personTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <StackPanel>
                        <TextBlock
                            Style="{StaticResource lblStyle}"
                            Text="First Name" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=FirstName}"/>
                        <TextBlock 
                            Style="{StaticResource lblStyle}"
                            Text="Age" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=Age}" />
                    </StackPanel>

                    <Image 
                        Margin="4"
                        Grid.Column="1" 
                        Width="96"
                        Height="140"
                        Source="{Binding Path=Photo}"/>
                </Grid>
        </DataTemplate>


    </Window.Resources>

    <Grid>


        <ListBox
            Margin="10"
            ItemsSource="{Binding Source={StaticResource people}}"/>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class Employee
    {
        public string FirstName
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }
        public string Photo
        {
            get;
            set;
        }

        public override string ToString()
        {
            return FirstName;
        }
    }

    public class People : Collection<Employee>
    {
        public People()
        {
            this.Add(new Employee()
                         {
                             FirstName = "A",
                             Age = 26,
                             Photo = "a.png"
                         });
            this.Add(new Employee()
                         {
                             FirstName = "C",
                             Age = 24,
                             Photo = "c.png"
                         });
        }
    }
}

   
    
     


ListBox binds to the people collection, and sets the DataTemplate to use for displaying each item

   
  

<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="300" Width="300">

    <Window.Resources>
        <WpfApplication1:People x:Key="people"/>
        
        <DataTemplate x:Key="personTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <StackPanel>
                        <TextBlock
                            Style="{StaticResource lblStyle}"
                            Text="First Name" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=FirstName}"/>
                        <TextBlock 
                            Style="{StaticResource lblStyle}"
                            Text="Age" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=Age}" />
                    </StackPanel>

                    <Image 
                        Margin="4"
                        Grid.Column="1" 
                        Width="96"
                        Height="140"
                        Source="{Binding Path=Photo}"/>
                </Grid>
        </DataTemplate>


    </Window.Resources>

    <Grid>
        <ListBox
            ItemsSource="{Binding Source={StaticResource people}}"
            ItemTemplate="{StaticResource personTemplate}"/>


    </Grid>
</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class Employee
    {
        public string FirstName
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }
        public string Photo
        {
            get;
            set;
        }

        public override string ToString()
        {
            return FirstName;
        }
    }

    public class People : Collection<Employee>
    {
        public People()
        {
            this.Add(new Employee()
                         {
                             FirstName = "A",
                             Age = 26,
                             Photo = "a.png"
                         });
            this.Add(new Employee()
                         {
                             FirstName = "C",
                             Age = 24,
                             Photo = "c.png"
                         });
        }
    }
}

   
    
     


Load the Items in a ListBox Asynchronously


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300"
    Loaded="Window_Loaded">

    <Window.Resources>
        <DataTemplate x:Key="ListItemTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="listBox" ItemTemplate= "{StaticResource ListItemTemplate}"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private ObservableCollection<string> numberDescriptions;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            numberDescriptions = new ObservableCollection<string>();

            listBox.ItemsSource = numberDescriptions;

            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,new LoadNumberDelegate(LoadNumber), 1);
        }
        private delegate void LoadNumberDelegate(int number);

        private void LoadNumber(int number)
        {
            numberDescriptions.Add("Number " + number.ToString());
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,new LoadNumberDelegate(LoadNumber), ++number);
        }
    }
}

   
    
     


Use DataTemplate in ListBox


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300"
    Loaded="Window_Loaded">

    <Window.Resources>
        <DataTemplate x:Key="ListItemTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="listBox" ItemTemplate= "{StaticResource ListItemTemplate}"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private ObservableCollection<string> numberDescriptions;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            numberDescriptions = new ObservableCollection<string>();

            listBox.ItemsSource = numberDescriptions;

            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,new LoadNumberDelegate(LoadNumber), 1);
        }
        private delegate void LoadNumberDelegate(int number);

        private void LoadNumber(int number)
        {
            numberDescriptions.Add("Number " + number.ToString());
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,new LoadNumberDelegate(LoadNumber), ++number);
        }
    }
}

   
    
     


Display Bounded objects onto ListBox























//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication1 {

public class Employee : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName) {
if( this.PropertyChanged != null ) {
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}

string name;
public string Name {
get { return this.name; }
set {
if( this.name == value ) { return; }
this.name = value;
Notify(“Name”);
}
}

int age;
public int Age {
get { return this.age; }
set {
if( this.age == value ) { return; }
this.age = value;
Notify(“Age”);
}
}

public Employee() { }
public Employee(string name, int age) {
this.name = name;
this.age = age;
}
}

class People : ObservableCollection { }

public partial class Window1 : System.Windows.Window {
public Window1() {
InitializeComponent();
}

}

public class AgeToRangeConverter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return (int)value < 25 ? "Under 25" : "Over 25"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } [/csharp]

Get Selected Item from ListBox


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="MyNameSpace.CompileXamlWindow.CompileXamlWindow"
        Title="Compile XAML Window" 
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanMinimize">
    <StackPanel>
        <Button HorizontalAlignment="Center" Margin="24" Click="ButtonOnClick">
            Click
        </Button>
        <Ellipse Name="elips" Width="200" Height="100" Margin="24" Stroke="Black"/>
        <ListBox Name="lstbox" Width="150" Height="150" Margin="24" SelectionChanged="ListBoxOnSelection" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MyNameSpace.CompileXamlWindow
{
    public partial class CompileXamlWindow : Window
    {

        public CompileXamlWindow()
        {
            InitializeComponent();
            
            foreach (PropertyInfo prop in typeof(Brushes).GetProperties())
                lstbox.Items.Add(prop.Name);
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Button btn = sender as Button;
            MessageBox.Show(btn.Content + "&#039; has been clicked.");
        }
        void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)
        {
            ListBox lstbox = sender as ListBox;
            string strItem = lstbox.SelectedItem as string;
            PropertyInfo prop = typeof(Brushes).GetProperty(strItem);
            elips.Fill = (Brush)prop.GetValue(null, null);
        }
    }
}

   
    
     


ListBox SelectionMode=Single


   
  
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ListBoxEvent"  x:Class="ListBoxEvent.Pane1">
  <Canvas.Resources>
    <src:myColors x:Key="Colors"/>
  </Canvas.Resources>

  <StackPanel>
      <TextBox Name="tb" Width="140" Height="30"></TextBox>
      <ListBox Name="lb" Width="100" Height="55" SelectionChanged="PrintText" SelectionMode="Single">
        <ListBoxItem>Item 1</ListBoxItem>
        <ListBoxItem>Item 2</ListBoxItem>
        <ListBoxItem>Item 3</ListBoxItem>
        <ListBoxItem>Item 4</ListBoxItem>
        <ListBoxItem>Item 5</ListBoxItem>
        <ListBoxItem>Item 6</ListBoxItem>
        <ListBoxItem>Item 7</ListBoxItem>
        <ListBoxItem>Item 8</ListBoxItem>
        <ListBoxItem>Item 9</ListBoxItem>
        <ListBoxItem>Item 10</ListBoxItem>
      </ListBox>
  </StackPanel>

</Canvas>

//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace ListBoxEvent
{
    public class myColors : ObservableCollection<string>
    {
        public myColors()
        {
            Add("A");
            Add("B");
            Add("C");
            Add("D");
            Add("E");
            Add("F");
        }
    }
    public partial class Pane1 : Canvas
    {

        public Pane1() : base(){
            InitializeComponent();
        }
        void PrintText(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
            tb.Text = "   You selected " + lbi.Content.ToString() + ".";
        }
    }
}