Change the Appearance of Alternate Items in 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"
    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));
        }
    }
}

   
    
     


Change the Appearance of a List Item When It's 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"
    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));
        }    
    }
}

   
    
     


Select Product Page Function

image_pdfimage_print


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

   
    
     


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