Select All ListBox Items

image_pdfimage_print


   
  
        
<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="300" Width="300">
    <StackPanel>
        <ListBox FontSize="16" Height="150" Margin="5" Name="listBox1" SelectionMode="Extended">
            <ListBoxItem>List Item 1</ListBoxItem>
            <ListBoxItem>List Item 2</ListBoxItem>
            <ListBoxItem>List Item 3</ListBoxItem>
        </ListBox>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <Label Content="_New item text:" VerticalAlignment="Center" Target="{Binding ElementName=textBox}"  />
            <TextBox Margin="5" Name="textBox" MinWidth="120" />            
        </StackPanel>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <Button Click="btnAddListItem_Click" Content="Add Item" IsDefault="True" Margin="5" Name="btnAddListItem" />
            <Button Click="btnDeleteListItem_Click" Content="Delete Items" Margin="5" Name="btnDeleteListItem" />
            <Button Click="btnSelectAll_Click" Content="Select All" Margin="5" Name="btnSelectAll" />
        </StackPanel>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void btnAddListItem_Click(object sender, RoutedEventArgs e)
        {
            if (textBox.Text.Length == 0)
            {
                MessageBox.Show("Enter text to add to the list.", Title);
            }
            else
            {
                ListBoxItem item = new ListBoxItem();
                item.Content = textBox.Text;
                item.IsSelected = true;
                item.HorizontalAlignment = HorizontalAlignment.Center;
                item.FontWeight = FontWeights.Bold;
                item.FontFamily = new FontFamily("Tahoma");

                listBox1.Items.Add(item);

                textBox.Clear();
                textBox.Focus();
            }
        }

        private void btnDeleteListItem_Click(object sender, RoutedEventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select list items to delete.", Title);
            }
            else
            {
                while (listBox1.SelectedItems.Count > 0)
                {
                    listBox1.Items.Remove(listBox1.SelectedItems[0]);
                }
            }
        }
        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            listBox1.SelectAll();
        }
    }
}

   
    
     


Create a style that will produce a horizontal ListBox.

image_pdfimage_print


   
  

<Window Background="cornsilk"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ListBox_Index.Window1">

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
      <Style TargetType="Separator">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type Separator}">
              <Border Width="2" Height="12" Margin="4" Background="Gray"/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

      <Style TargetType="ListBox">
        <Setter Property="ItemsPanel">
          <Setter.Value>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
          </Setter.Value>
        </Setter>
      </Style>
      
    </Grid.Resources>

    <ListBox Name="lb" Margin="10" Height="50" Grid.Column="0" Grid.Row="2" Grid.RowSpan="2" SelectionChanged="PrintText">
      <ListBoxItem>Item 1</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 2</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 3</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 4</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 5</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 6</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 7</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 8</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 9</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 10</ListBoxItem>
    </ListBox>
    <Label Margin="10, 10, 3, 3" Name="label1" Grid.Column="0" Grid.Row="5"/>


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

namespace ListBox_Index
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }  
        private void PrintText(object sender, SelectionChangedEventArgs args){
            ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
            if( lbi == null ) return;
            label1.Content = "You chose " + lbi.Content.ToString() + "."; 
        } 
    }
}

   
    
     


Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ThicknessConverter_Csharp.Window1"
    Title="ThicknessConverter Sample">
  <DockPanel Width="400" HorizontalAlignment="Left" VerticalAlignment="Top">
    <TextBlock DockPanel.Dock="Top" FontFamily="Verdana" FontSize="18" FontWeight="Bold" Margin="5">
      ThicknessConverter Sample</TextBlock>
    <Border Name="border1" Height="300" Width="300" Border.BorderThickness="2" Border.BorderBrush="Black" DockPanel.Dock="Top">
      <Grid Height="30" VerticalAlignment="Top">
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Name="bThickness"/>
        <TextBlock Grid.Row="1" Name="bColor"/>
      </Grid>
    </Border>
    <Grid DockPanel.Dock="Top">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" TextWrapping="Wrap">Change the BorderThickness Property:</TextBlock>
      <ListBox VerticalAlignment="Top" Grid.Column="1" Grid.Row="1" Width="50" Height="50" Margin="5" SelectionChanged="changeThickness">
        <ListBoxItem>0</ListBoxItem>
        <ListBoxItem>5</ListBoxItem>
        <ListBoxItem>10</ListBoxItem>
        <ListBoxItem>15</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
        <ListBoxItem>25</ListBoxItem>
        <ListBoxItem>30</ListBoxItem>
        <ListBoxItem>35</ListBoxItem>
        <ListBoxItem>40</ListBoxItem>
        <ListBoxItem>45</ListBoxItem>
        <ListBoxItem>50</ListBoxItem>
      </ListBox>
    </Grid>
  </DockPanel>        
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Media;

namespace ThicknessConverter_Csharp
{
  public partial class Window1 : Window
  {
        public void changeThickness(object sender, SelectionChangedEventArgs args)
        {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
      ThicknessConverter myThicknessConverter = new ThicknessConverter();
      Thickness th1 = (Thickness)myThicknessConverter.ConvertFromString(li.Content.ToString());
            border1.BorderThickness = th1;
            bThickness.Text = "Border.BorderThickness =" + li.Content.ToString();
        }
  }
}

   
    
     


Convert contents of a ListBoxItem to an instance of Thickness by using the BrushConverter

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ThicknessConverter_Csharp.Window1"
    Title="ThicknessConverter Sample">
  <DockPanel Width="400" HorizontalAlignment="Left" VerticalAlignment="Top">
    <TextBlock DockPanel.Dock="Top" FontFamily="Verdana" FontSize="18" FontWeight="Bold" Margin="5">
      ThicknessConverter Sample</TextBlock>
    <Border Name="border1" Height="300" Width="300" Border.BorderThickness="2" Border.BorderBrush="Black" DockPanel.Dock="Top">
      <Grid Height="30" VerticalAlignment="Top">
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Name="bThickness"/>
        <TextBlock Grid.Row="1" Name="bColor"/>
      </Grid>
    </Border>
    <Grid DockPanel.Dock="Top">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="1" Grid.Column="2" TextWrapping="Wrap">Set the BorderBrush Property:</TextBlock>
      <ListBox VerticalAlignment="Top" Grid.Column="3" Grid.Row="1" Width="60" Height="50" Margin="5" SelectionChanged="changeColor">
        <ListBoxItem>Red</ListBoxItem>
        <ListBoxItem>Green</ListBoxItem>
        <ListBoxItem>Blue</ListBoxItem>
        <ListBoxItem>Yellow</ListBoxItem>
        <ListBoxItem>Orange</ListBoxItem>
        <ListBoxItem>Purple</ListBoxItem>
        <ListBoxItem>Silver</ListBoxItem>
        <ListBoxItem>Pink</ListBoxItem>
        <ListBoxItem>Maroon</ListBoxItem>
        <ListBoxItem>Brown</ListBoxItem>
        <ListBoxItem>Black</ListBoxItem>
      </ListBox>
    </Grid>
  </DockPanel>        
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Media;

namespace ThicknessConverter_Csharp
{
  public partial class Window1 : Window
  {

    public void changeColor(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            BrushConverter myBrushConverter = new BrushConverter();
            border1.BorderBrush = (Brush)myBrushConverter.ConvertFromString((string)li2.Content);
            bColor.Text = "Border.Borderbrush =" + li2.Content.ToString();
        }
  }
}

   
    
     


DataTrigger, ListBox and user object

image_pdfimage_print


   
  

<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"));
        }
    }

}

   
    
     


Add selected file to ListBox

image_pdfimage_print


   
  

<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);
                }                
            }
        }
    }
}

   
    
     


ListBox with Image item

image_pdfimage_print


   
  
<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)
        {
        }
    }
}