View and Select Items Using a List

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

   
    
     


Set text to TextBlock for selected list item

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

   
    
     


Iterate through the selected items and remove each one

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

   
    
     


Ensure there is at least one item selected

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 ListView control that implements a GridView view with CheckBox controls for each row.

image_pdfimage_print


   
   

<Window x:Class="SDKSample.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
    <XmlDataProvider x:Key="MyData" XPath="/Info">
      <x:XData>
        <Info xmlns="">
          <Song Name="Song 1"/>
          <Song Name="Song 2"/>
          <Song Name="Song 3"/>
        </Info>
      </x:XData>
    </XmlDataProvider>
    <Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
          <Setter Property="Foreground" Value="Blue" />
          <Setter Property="Cursor" Value="Hand"/>
        </Trigger>
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="true" />
            <Condition Property="Selector.IsSelectionActive" Value="true" />
          </MultiTrigger.Conditions>
          <Setter Property="Foreground" Value="Yellow" />
        </MultiTrigger>
      </Style.Triggers>
    </Style>
    <DataTemplate x:Key="FirstCell">
      <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Path=IsSelected, 
          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <StackPanel>
    <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=Song}" 
           ItemContainerStyle="{StaticResource MyContainer}" 
           SelectionMode="Single" 
           Name="myPlaylist">
      <ListView.View>
        <GridView>
          <GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="30"/>
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=@Name}" Width="80"/>
        </GridView>
      </ListView.View>
    </ListView>
  </StackPanel>
</Window>

   
    
    
     


Use ArrayList as the ListView ItemSource

image_pdfimage_print


   
   

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Year}" 
                          Header="Year"
                          Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Month}" 
                          Header="Month"
                          Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Day}" 
                          Header="Day"
                          Width="50"/>
                </GridView>
            </ListView.View>
            <ListView.ItemsSource>
                <col:ArrayList>
                    <sys:DateTime>2006/1/1</sys:DateTime>
                    <sys:DateTime>2006/1/2</sys:DateTime>
                </col:ArrayList>
            </ListView.ItemsSource>
        </ListView>
    </StackPanel>
</Window>

   
    
    
     


Create a ListView control that implements a GridView view mode, displays content in groups.

image_pdfimage_print


   
   

<Window xmlns=&#039;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#039;
        xmlns:x=&#039;http://schemas.microsoft.com/winfx/2006/xaml&#039;>
    <Window.Resources>
        <XmlDataProvider x:Key="MyData" XPath="/Info">
            <x:XData>
                <Info xmlns="">
                    <Item ID="1" Name="Book 1" Price="$2.05" Author="Author A" Catalog="Business"/>
                    <Item ID="2" Name="Book 2" Price="$0.00" Author="Author B" Catalog="Language"/>
                    <Item ID="3" Name="Book 3" Price="$19.00" Author="Author C" Catalog="Language"/>
                    <Item ID="4" Name="Book 4" Price="$81.50" Author="Author D" Catalog="Business"/>
                    <Item ID="5" Name="Book 5" Price="$9.00" Author="Author E" Catalog="Health"/>
                    <Item ID="6" Name="Book 6" Price="$18.50" Author="Author F" Catalog="Language"/>
                </Info>
            </x:XData>
        </XmlDataProvider>
        <CollectionViewSource x:Key=&#039;src&#039; Source="{Binding Source={StaticResource MyData}, XPath=Item}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Catalog" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <ListView ItemsSource=&#039;{Binding Source={StaticResource src}}&#039; BorderThickness="0">
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" BorderBrush="#FFA4B97F" 
                            BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" 
                                   Margin="5,0,0,0" Width="100"/>
                                                <TextBlock FontWeight="Bold" 
                                   Text="{Binding Path=ItemCount}"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="ID" 
                        DisplayMemberBinding="{Binding XPath=@ID}" 
                        Width="200" />
                <GridViewColumn Header="Name" 
                        DisplayMemberBinding="{Binding XPath=@Name}" 
                        Width="240" />
                <GridViewColumn Header="Price" 
                        DisplayMemberBinding="{Binding XPath=@Price}"
                        Width="180" />
                <GridViewColumn Header="Author" 
                        DisplayMemberBinding="{Binding XPath=@Author}" 
                        Width="180" />
            </GridView>
        </ListView.View>
    </ListView>


</Window>