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

   
    
     


ListBox SelectionChanged Event


   
  

<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 SelectionChanged="OuterListBox_SelectionChanged" Name="outerListBox">
            <ListBoxItem Content="Item 1" FontFamily="Tahoma" HorizontalContentAlignment="Left" />
            <ListBoxItem Content="Item 2" FontFamily="Algerian" FontSize="16" HorizontalContentAlignment="Center" />
            <ListBoxItem Content="Item 3" FontSize="20" HorizontalContentAlignment="Right" />
            <Button Content="Button directly in a list" Margin="5" />
            <ListBoxItem HorizontalContentAlignment="Center" Margin="5">
                <Button Content="Button wrapped in ListBoxItem" />
            </ListBoxItem>
            <ListBox Height="50" Margin="5">
                <ListBoxItem Content="Inner List Item 1" Selected="InnerListBoxItem_Selected" />
                <ListBoxItem Content="Inner List Item 2" Selected="InnerListBoxItem_Selected" />
                <ListBoxItem Content="Inner List Item 3" Selected="InnerListBoxItem_Selected" />
                <ListBoxItem Content="Inner List Item 4" Selected="InnerListBoxItem_Selected" />
            </ListBox>
            <StackPanel Margin="5" Orientation="Horizontal">
                <Label Content="Enter some text:" />
                <TextBox MinWidth="150" />
            </StackPanel>
        </ListBox>
        <TextBlock Text="No item currently selected." Margin="10" HorizontalAlignment="Center" Name="txtSelectedItem" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void InnerListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = e.OriginalSource as ListBoxItem;

            if (item != null)
            {
                MessageBox.Show(item.Content + " was selected.", Title);
            }
        }

        private void OuterListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object item = outerListBox.SelectedItem;

            if (item == null)
            {
                txtSelectedItem.Text = "No item currently selected.";
            }
            else
            {
                txtSelectedItem.Text = item.ToString();
            }
        }
    }
}

   
    
     


Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="FontSizeConverter_Csharp.Window1"
    Title="FontSizeConverter Sample">
<StackPanel>
        <TextBlock Name="text1" TextWrapping="Wrap" Height="300" Width="500">
            this is a test
        </TextBlock>
    <ListBox Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeSize">
        <ListBoxItem>5</ListBoxItem>
        <ListBoxItem>8</ListBoxItem>
        <ListBoxItem>11</ListBoxItem>
        <ListBoxItem>14</ListBoxItem>
        <ListBoxItem>17</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
        <ListBoxItem>23</ListBoxItem>
        <ListBoxItem>26</ListBoxItem>
        <ListBoxItem>29</ListBoxItem>
        <ListBoxItem>32</ListBoxItem>
    </ListBox>
    <ListBox Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeFamily">
      <ListBoxItem>Arial</ListBoxItem>
      <ListBoxItem>Courier New</ListBoxItem>
      <ListBoxItem>Tahoma</ListBoxItem>
      <ListBoxItem>Times New Roman</ListBoxItem>
      <ListBoxItem>Verdana</ListBoxItem>
    </ListBox>      
   
  </StackPanel>        
</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 FontSizeConverter_Csharp
{
  public partial class Window1 : Window
  {
        public void changeSize(object sender, SelectionChangedEventArgs args)
        {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
      FontSizeConverter myFontSizeConverter = new FontSizeConverter();
      text1.FontSize = (Double)myFontSizeConverter.ConvertFromString(li.Content.ToString());
        }

    public void changeFamily(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            text1.FontFamily = new System.Windows.Media.FontFamily(li2.Content.ToString());
        }
  }
}

   
    
     


Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter


   
  

<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="GridLengthConverter_grid.Window1"
    Title="GridLengthConverter Sample"
    WindowState="Maximized">
  <StackPanel>
      <Grid Name="grid1" ShowGridLines="True" Height="300">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1" />
            <ColumnDefinition Name="col2" />
            <ColumnDefinition Name="col3" />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Name="row1" />
            <RowDefinition Name="row2" />
            <RowDefinition Name="row3" />
          </Grid.RowDefinitions>
          <TextBlock Grid.Row="0" Grid.Column="0" TextWrapping="Wrap">Column 0, Row 0</TextBlock>
          <TextBlock Grid.Row="0" Grid.Column="1" TextWrapping="Wrap">Column 1, Row 0</TextBlock>
          <TextBlock Grid.Row="0" Grid.Column="2" TextWrapping="Wrap">Column 2, Row 0</TextBlock>
          <TextBlock Grid.Row="1" Grid.Column="0" TextWrapping="Wrap">Column 0, Row 1</TextBlock>
          <TextBlock Grid.Row="1" Grid.Column="1" TextWrapping="Wrap">Column 1, Row 1</TextBlock>
          <TextBlock Grid.Row="1" Grid.Column="2" TextWrapping="Wrap">Column 2, Row 1</TextBlock>
          <TextBlock Grid.Row="2" Grid.Column="0" TextWrapping="Wrap">Column 0, Row 2</TextBlock>
          <TextBlock Grid.Row="2" Grid.Column="1" TextWrapping="Wrap">Column 1, Row 2</TextBlock>
          <TextBlock Grid.Row="2" Grid.Column="2" TextWrapping="Wrap">Column 2, Row 2</TextBlock>
     </Grid>
       <Slider Name="hs1" Width="100" IsSnapToTickEnabled="True" Maximum="2" TickPlacement="BottomRight" TickFrequency="1" ValueChanged="changeColVal"/>
       <Slider Orientation="Horizontal" Name="hs2" Width="100" IsSnapToTickEnabled="True" Maximum="2" TickPlacement="BottomRight" TickFrequency="1" ValueChanged="changeRowVal" />
       <TextBlock Name="txt1" TextWrapping="Wrap"/>
       <TextBlock Name="txt2" TextWrapping="Wrap"/>
       <ListBox Width="50" Height="50" SelectionChanged="changeCol">
           <ListBoxItem>25</ListBoxItem>
           <ListBoxItem>50</ListBoxItem>
           <ListBoxItem>75</ListBoxItem>
           <ListBoxItem>100</ListBoxItem>
           <ListBoxItem>125</ListBoxItem>
           <ListBoxItem>150</ListBoxItem>        
      </ListBox>

        <ListBox Width="50" Height="50" SelectionChanged="changeRow">
          <ListBoxItem>25</ListBoxItem>
          <ListBoxItem>50</ListBoxItem>
          <ListBoxItem>75</ListBoxItem>
          <ListBoxItem>100</ListBoxItem>
          <ListBoxItem>125</ListBoxItem>
          <ListBoxItem>150</ListBoxItem>
        </ListBox>      
        <Button Name="btn1" Click="setMinWidth">Set MinWidth="100"</Button>
        <Button Name="btn2" Click="setMaxWidth">Set MaxWidth="125"</Button>
        <Button Name="btn3" Click="setMinHeight">Set MinHeight="50"</Button>
        <Button Name="btn4" Click="setMaxHeight">Set MaxHeight="75"</Button> 
    </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace GridLengthConverter_grid
{
  public partial class Window1 : Window
  {
        
        public void changeRowVal(object sender, RoutedEventArgs e)
        {
            txt2.Text = "Current Grid Row is " + hs2.Value.ToString();
        }
        
        public void changeColVal(object sender, RoutedEventArgs e) 
    {
            txt1.Text = "Current Grid Column is " + hs1.Value.ToString();
        }
        
        public void changeCol(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            GridLengthConverter myGridLengthConverter = new GridLengthConverter();
            if (hs1.Value == 0)
            {
                GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col1.Width = gl1;
            }
            else if (hs1.Value == 1)
            {
                GridLength gl2 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col2.Width = gl2;
            }
            else if (hs1.Value == 2)
            {
                GridLength gl3 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col3.Width = gl3;
            }
        }
        public void changeRow(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            GridLengthConverter myGridLengthConverter2 = new GridLengthConverter();

            if (hs2.Value == 0)
            {
                GridLength gl4 = (GridLength)myGridLengthConverter2.ConvertFromString(li2.Content.ToString());
                row1.Height = gl4;
            }
             else if (hs2.Value == 1)
            {
                GridLength gl5 = (GridLength)myGridLengthConverter2.ConvertFromString(li2.Content.ToString());
                row2.Height = gl5;
            }
             else if (hs2.Value == 2)
            {
               GridLength gl6 = (GridLength)myGridLengthConverter2.ConvertFromString(li2.Content.ToString());
               row3.Height = gl6;
            }

        }

        public void setMinWidth(object sender, RoutedEventArgs e)
        {
            col1.MinWidth = 100;
            col2.MinWidth = 100;
            col3.MinWidth = 100;
        }
        public void setMaxWidth(object sender, RoutedEventArgs e)
        {
            col1.MaxWidth = 125;
            col2.MaxWidth = 125;
            col3.MaxWidth = 125;
        }
        public void setMinHeight(object sender, RoutedEventArgs e)
        {
            row1.MinHeight = 50;
            row2.MinHeight = 50;
            row3.MinHeight = 50;
        }
        public void setMaxHeight(object sender, RoutedEventArgs e)
        {
            row1.MaxHeight = 75;
            row2.MaxHeight = 75;
            row3.MaxHeight = 75;
        }

    }
}

   
    
     


ListBox Selected Index, Item, Value


   
  

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WpfApplication1" Height="399" Width="320" WindowStartupLocation="CenterScreen">
    <StackPanel xmlns:CorLib = "clr-namespace:System;assembly=mscorlib">
    <ListBox Name = "dataList"/>
    <ListBox Name = "lstColors">
      <StackPanel Orientation ="Horizontal" Tag ="Yellow">
        <Ellipse Fill ="Yellow" Height ="50" Width ="50"/>
        <Label FontSize ="20" HorizontalAlignment="Center"
          VerticalAlignment="Center">Yellow</Label>
      </StackPanel>
      <StackPanel Orientation ="Horizontal" Tag ="Blue">
        <Ellipse Fill ="Blue" Height ="50" Width ="50"/>
        <Label FontSize ="20" HorizontalAlignment="Center"
          VerticalAlignment="Center">Blue</Label>
      </StackPanel>
      <StackPanel Orientation ="Horizontal" Tag ="Green">
        <Ellipse Fill ="Green" Height ="50" Width ="50"/>
        <Label FontSize ="20" HorizontalAlignment="Center"
          VerticalAlignment="Center">Green</Label>
      </StackPanel>
    </ListBox>
    <Button Name ="btnGetGameSystem" Click ="btnGetGameSystem_Click">
      Video
    </Button>
    <Button Name ="btnGetColor" Click ="btnGetColor_Click">
      Get Color
    </Button>
  </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;


namespace WpfApplication1
{
  public partial class MainWindow : System.Windows.Window
  {

    public MainWindow()
    {
      InitializeComponent();
      dataList.Items.Add("A");
      dataList.Items.Add("B");
      dataList.Items.Add("C");
      dataList.Items.Add("D");
      dataList.Items.Add("E");
    } 
    protected void btnGetGameSystem_Click(object sender, RoutedEventArgs args)
    {
      Console.WriteLine(dataList.SelectedIndex);
      Console.WriteLine(dataList.SelectedItem);
      Console.WriteLine(dataList.SelectedValue);
    }

    protected void btnGetColor_Click(object sender, RoutedEventArgs args)
    {
      Console.WriteLine((lstColors.Items[lstColors.SelectedIndex] as StackPanel).Tag);
    }
  }
}

   
    
     


ListBox and SelectionMode




Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8




//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ControlDemos
{
public partial class listbox : Window
{
public listbox()
{
InitializeComponent();
}
public void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
int nCount = listBox1.SelectedItems.Count;
for (int lp = 0; lp < nCount; lp++) Console.WriteLine(listBox1.SelectedItems[lp].ToString()); } } } [/csharp]