List box that uses data binding to populate the list box items.


   
  

<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 Margin="10, 10, 3, 3">
      <WrapPanel Width="500" Orientation="Horizontal" Name="rectanglesPanel">
        <WrapPanel.Resources>
          <Style TargetType="Rectangle">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="20"/>
            <Setter Property="Margin" Value="5"/>
          </Style>
        </WrapPanel.Resources>
      </WrapPanel>
      <ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
            Width="265" Height="55" Background="HoneyDew" SelectionChanged="myListBox_SelectionChanged"
            ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
      </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("LightBlue");
            Add("Pink");
            Add("Red");
            Add("Purple");
            Add("Blue");
            Add("Green");
        }
    }
    public partial class Pane1 : Canvas
    {
        public Pane1() : base()
        {
            InitializeComponent();
        }
        void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            BrushConverter converter = new BrushConverter();
            foreach (string color in args.AddedItems)
            {
                if (GetRectangle(color) == null)
                {
                    Rectangle aRect = new Rectangle();
                    aRect.Fill = (Brush) converter.ConvertFrom(color);
                    aRect.Tag = color;
                    rectanglesPanel.Children.Add(aRect);
                }

            }
            foreach (string color in args.RemovedItems)
            {
                FrameworkElement removedItem = GetRectangle(color);
                if (removedItem != null)
                {
                    rectanglesPanel.Children.Remove(removedItem);
                }
            }
        }

        FrameworkElement GetRectangle(string color)
        {
            foreach (FrameworkElement rect in rectanglesPanel.Children)
            {
                if (rect.Tag.ToString() == color)
                    return rect;
            }

            return null;
        }

    }

}

   
    
     


This list box allows multiple user selections.


   
  

<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">


  <StackPanel>
    <DockPanel Margin="10, 10, 3, 3" >
      <ListBox SelectionMode="Multiple">
        <DockPanel>
          <Image Source="datacat.png"/>
          <TextBlock>CAT</TextBlock>
        </DockPanel>
        <DockPanel>
          <Image Source="datadog.png"/>
          <TextBlock>DOG</TextBlock>
        </DockPanel>
        <DockPanel>
          <Image Source="datafish.png"/>
          <TextBlock>FISH</TextBlock>
        </DockPanel>
      </ListBox>
    </DockPanel>
  </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 partial class Pane1 : Canvas
    {

        public Pane1() : base()
        {
            InitializeComponent();
        }
    }

}

   
    
     


List box with text and non-text content in the list box items.


   
  

<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">


  <StackPanel>
    <DockPanel Margin="10, 10, 3, 3" >
      <ListBox SelectionMode="Multiple">
        <DockPanel>
          <Image Source="datacat.png"/>
          <TextBlock>CAT</TextBlock>
        </DockPanel>
        <DockPanel>
          <Image Source="datadog.png"/>
          <TextBlock>DOG</TextBlock>
        </DockPanel>
        <DockPanel>
          <Image Source="datafish.png"/>
          <TextBlock>FISH</TextBlock>
        </DockPanel>
      </ListBox>
    </DockPanel>
  </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 partial class Pane1 : Canvas
    {

        public Pane1() : base()
        {
            InitializeComponent();
        }
    }

}

   
    
     


Fill up the ListBox with brush names


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.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 WpfApplication1
{
    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 with Image item


   
  
<Window x:Class="ClassicControls.ImageList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ImageList" Height="300" Width="300">
  <ListBox Margin="5" SelectionMode="Multiple" Name="lst"  SelectionChanged="lst_SelectionChanged">
    <StackPanel Orientation="Horizontal">
      <Image Source="c:image.jpg"  Width="30" Height="30"></Image>
      <Label VerticalContentAlignment="Center">A happy face</Label>
    </StackPanel>

  </ListBox>
</Window>
//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;

namespace ClassicControls
{
    public partial class ImageList : System.Windows.Window
    {
        public ImageList()
        {
            InitializeComponent();
        }
        private void lst_SelectionChanged(object sender, RoutedEventArgs e)
        {
        }
    }
}

   
    
     


Add selected file to ListBox


   
  

<Window x:Class="Windows.OpenFileTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OpenFileTest" Height="300" Width="300" 
    >
  <DockPanel Margin="5">
    <Button DockPanel.Dock="Top" Click="cmdOpen_Click">Open</Button>
    <ListBox Name="lstFiles"></ListBox>
  </DockPanel>
</Window>
//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 Microsoft.Win32;

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

        private void cmdOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog myDialog = new OpenFileDialog();

            myDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            myDialog.CheckFileExists = true;
            myDialog.Multiselect = true;

            if (myDialog.ShowDialog() == true)
            {
                lstFiles.Items.Clear();
                foreach (string file in myDialog.FileNames)
                {
                    lstFiles.Items.Add(file);
                }                
            }
        }
    }
}

   
    
     


DataTrigger, ListBox and user object


   
  

<Window x:Class="DataTriggerSample.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:DataTriggerSample"
  Title="DataTriggerSample" Height="300" Width="300">
  <Window.Resources>
    <c:People x:Key="PeopleData"/>

    <Style TargetType="{x:Type ListBoxItem}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Name}" Value="A">
          <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
        <MultiDataTrigger>
          <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding Path=Name}" Value="S" />
          </MultiDataTrigger.Conditions>
          <MultiDataTrigger.Setters>
            <Setter Property="Background" Value="LightGreen" />
          </MultiDataTrigger.Setters>
        </MultiDataTrigger>
      </Style.Triggers>
    </Style>

    <DataTemplate DataType="{x:Type c:Person}">
      <Canvas Width="260" Height="20">
        <TextBlock FontSize="12" Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
      </Canvas>
    </DataTemplate>
  </Window.Resources>

  <StackPanel>
    <TextBlock FontSize="18" Margin="5" FontWeight="Bold"
      HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
    <ListBox Width="250" HorizontalAlignment="Center" Background="White"
      ItemsSource="{Binding Source={StaticResource PeopleData}}"/>
  </StackPanel>

</Window>
//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.Collections.ObjectModel;

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

    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Person(string name)
        {
            this._name = name;
        }
    }

    public class People : ObservableCollection<Person>
    {
        public People()
        {
            Add(new Person("A"));
            Add(new Person("B"));
            Add(new Person("C"));
        }
    }

}