View and Select Items Using a List


   
  

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

   
    
     


Select Product Page Function


   
  
<PageFunction
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    x:Class="NavigationApplication.SelectProductPageFunction"    
    Title="SelectProductPageFunction"
    xmlns:local="clr-namespace:NavigationApplication"
    x:TypeArguments="local:Product">
  <StackPanel>
    <ListBox Name="lstProducts">
      <ListBoxItem>A</ListBoxItem>
      <ListBoxItem>B</ListBoxItem>
      <ListBoxItem>C</ListBoxItem>
    </ListBox>

    
      <TextBlock Grid.Row="1">
        <Hyperlink Click="lnkOK_Click">OK</Hyperlink>
        <Hyperlink Click="lnkCancel_Click">Cancel</Hyperlink>
      </TextBlock>
    
  </StackPanel>
</PageFunction>
//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.Navigation;
using System.Windows.Shapes;


namespace NavigationApplication
{
    public partial class SelectProductPageFunction 
    {
        public SelectProductPageFunction()
        {
            InitializeComponent();
        }

        private void lnkOK_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = (ListBoxItem)lstProducts.SelectedItem;            
            Product product = new Product(item.Content.ToString());
            OnReturn(new ReturnEventArgs<Product>(product));
        }
        private void lnkCancel_Click(object sender, RoutedEventArgs e)
        {
            OnReturn(null);
        }
    }
    public class Product
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Product(string name)
        {
            Name = name;
        }
    }    
}

   
    
     


Change the Appearance of a List Item When It's Selected


   
  

<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;assembly="
    Title="WPF" Height="248" Width="128">
    <Window.Resources>

        <WpfApplication1:Countries x:Key="countries"/>

        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Content" Value="{Binding Path=Name}"/>
            <Setter Property="Margin" Value="20"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="FontSize" Value="14" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource countries}}"/>
    </Grid>

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

namespace WpfApplication1
{
    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }

    }

    public enum Continent
    {
        Europe,
        NorthAmerica,
    }

    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }    
    }
}

   
    
     


Change the Appearance of Alternate Items in a List


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="248" Width="128">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <Style x:Key="AlternateStyle">
            <Setter Property="ListBoxItem.Background" Value="LightGray"/>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource countries}}" >
            <ListBox.ItemContainerStyleSelector>
                <local:AlternatingRowStyleSelector AlternateStyle="{StaticResource AlternateStyle}" />
            </ListBox.ItemContainerStyleSelector>
        </ListBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
    public class AlternatingRowStyleSelector : StyleSelector
    {
        public Style DefaultStyle
        {
            get;
            set;
        }

        public Style AlternateStyle
        {
            get;
            set;
        }

        private bool isAlternate = false;

        public override Style SelectStyle(object item, DependencyObject container)
        {
            Style style = isAlternate ? AlternateStyle : DefaultStyle;

            isAlternate = !isAlternate;

            return style;
        }
    }

    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }
    }
    public enum Continent
    {
        Europe,
        NorthAmerica,
    }
    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }
    }
}

   
    
     


Get selected item count from ListBox


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="AboutDialog" ListBox.SelectionChanged="ListBox_SelectionChanged"
        Button.Click="Button_Click"
        Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
        Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF
    </Label>
    <Label>License</Label>
    <Label>Installed Dll:</Label>
    <ListBox>
      <ListBoxItem>1</ListBoxItem>
      <ListBoxItem>2</ListBoxItem>
      <ListBoxItem>3</ListBoxItem>
      <ListBoxItem>4</ListBoxItem>
      <ListBoxItem>5</ListBoxItem>
      <ListBoxItem>6</ListBoxItem>
      <ListBoxItem>7</ListBoxItem>
      <ListBoxItem>8</ListBoxItem>
      <ListBoxItem>9</ListBoxItem>
    </ListBox>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>test</StatusBar>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

public partial class AboutDialog : Window
{
    public AboutDialog()
    {
        InitializeComponent();
    }

    void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
            MessageBox.Show("You just selected " + e.AddedItems[0]);
    }

    void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("You just clicked " + e.Source);
    }
}

   
    
     


Select All and unselect all


   
  

<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">
      <ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
            Width="265" Height="55"
            ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
      </ListBox>
      <StackPanel Orientation="Horizontal" Margin="10">
        <Button Content="_Select all" Click="selectAll_Click"/>
        <Button Content="_Unselect all" Click="unselectAll_Click"/>
      </StackPanel>
    </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 selectAll_Click(object sender, RoutedEventArgs e)
        {
            myListBox.SelectAll();
        }

        void unselectAll_Click(object sender, RoutedEventArgs e)
        {
            myListBox.UnselectAll();
        }
    }

}

   
    
     


This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.


   
  

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

    }

}