ListBox SelectionChanged Event

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

   
    
     


ListBox SelectionMode=Single

image_pdfimage_print


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

   
    
     


Get Selected Item from ListBox

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="MyNameSpace.CompileXamlWindow.CompileXamlWindow"
        Title="Compile XAML Window" 
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanMinimize">
    <StackPanel>
        <Button HorizontalAlignment="Center" Margin="24" Click="ButtonOnClick">
            Click
        </Button>
        <Ellipse Name="elips" Width="200" Height="100" Margin="24" Stroke="Black"/>
        <ListBox Name="lstbox" Width="150" Height="150" Margin="24" SelectionChanged="ListBoxOnSelection" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MyNameSpace.CompileXamlWindow
{
    public partial class CompileXamlWindow : Window
    {

        public CompileXamlWindow()
        {
            InitializeComponent();
            
            foreach (PropertyInfo prop in typeof(Brushes).GetProperties())
                lstbox.Items.Add(prop.Name);
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Button btn = sender as Button;
            MessageBox.Show(btn.Content + "&#039; has been clicked.");
        }
        void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)
        {
            ListBox lstbox = sender as ListBox;
            string strItem = lstbox.SelectedItem as string;
            PropertyInfo prop = typeof(Brushes).GetProperty(strItem);
            elips.Fill = (Brush)prop.GetValue(null, null);
        }
    }
}

   
    
     


Diagonal linear gradient multiple colors 2

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel Grid.Column="1" Grid.Row="2">
      <TextBlock Margin="5,10,5,0" Text="Diagonal linear gradient" />
      <TextBlock Margin="5,0,5,5" Text="- multiple colors" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="1,1">
            <GradientStop Color="Red" Offset="0.2" />
            <GradientStop Color="Yellow" Offset="0.3" />
            <GradientStop Color="Coral" Offset="0.4" />
            <GradientStop Color="Blue" Offset="0.5" />
            <GradientStop Color="White" Offset="0.6" />
            <GradientStop Color="Green" Offset="0.7" />
            <GradientStop Color="Purple" Offset="0.8" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Diagonal linear gradient with 0.5 Offset for White

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel>
      <TextBlock Margin="5,10,5,0"
        Text="Diagonal linear gradient" />
      <TextBlock Margin="5,0,5,5"
        Text="- with 0.5 Offset for White" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="1,1">
            <GradientStop Color="Green" Offset="0" />
            <GradientStop Color="White" Offset="0.5" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Horizontal linear gradient

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel Grid.Column="1" Grid.Row="0">
      <TextBlock Margin="5" Text="Horizontal linear gradient:" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="0,1">
            <GradientStop Color="Red" Offset="0" />
            <GradientStop Color="White" Offset="1" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Vertical linear gradient

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel>
      <TextBlock Margin="5" Text="Vertical linear gradient:" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
            <GradientStop Color="Blue" Offset="0" />
            <GradientStop Color="Yellow" Offset="1" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>