Class: ListBox

Namespace: System.Windows.Controls

Library: PresentationFramework

For the creation and use of a ListBox you will need to use another Class: ListBoxItem

Better to play a real demo looking the real code.

The Window1.xaml file is:

<Window  
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    x:Class="DMU.Window1"
    Title="DMU Tutorials Serie"
    Top="100"
    Left= "100"
    Width="400"
    Height="400"
>

 <Canvas  Width="400"  Height="400" Background="DeepSkyBlue"  >
  <TextBlock  Name="Text1"   Canvas.Top="50" Canvas.Left="100"   FontSize="14">Press buttons to populate list</TextBlock>
  <Button   Name="Bt1" Canvas.Top="100" Canvas.Left="100" Click = "Hat">Hat</Button>
  <Button  Name="Bt2" Canvas.Top="100" Canvas.Left="200" Click = "Shoe">Shoe</Button>
  <Button   Name="Bt3" Canvas.Top="100" Canvas.Left="300" Click = "Shirt">Shirt</Button>
   <ListBox Name="TheLB" Canvas.Top="150" Canvas.Left="100" Width="100" Height="55" SelectionMode="Single" SelectionChanged="Print" >
    <ListBoxItem>Trousers</ListBoxItem>
   </ListBox>
   <TextBlock    Canvas.Top="250" Canvas.Left="100"   FontSize="14"  >Click one to select</TextBlock>
   <TextBox Name = "TheItem" Canvas.Top="300" Canvas.Left="100" FontSize="14"/>
 </Canvas>
</Window> 

And the Window1.xaml.cs file is:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
 
namespace DMU{
	 
public partial class Window1 : Window{
  
  public Window1(){
   InitializeComponent();
  }
  private void Hat(object sender, RoutedEventArgs e){
     ListBoxItem li  = new ListBoxItem();
     li.Content =  "Hat" ;
     TheLB.Items.Add(li);
     Bt1.IsEnabled = false;
   }
  private void Shoe(object sender, RoutedEventArgs e){
     ListBoxItem li  = new ListBoxItem();
     li.Content = "Shoe" ;
     TheLB.Items.Add(li);
     Bt2.IsEnabled = false;
  }
  private void Shirt(object sender, RoutedEventArgs e){
    ListBoxItem li = new ListBoxItem();
    li.Content = "Shirt";
    TheLB.Items.Add(li);
    Bt3.IsEnabled = false;
  }
  void Print(object sender, SelectionChangedEventArgs args){
   ListBoxItem lboxitem = new ListBoxItem();
   lboxitem = TheLB.SelectedItem as ListBoxItem;
   TheItem.Text = "Selected: " + lboxitem.Content.ToString();

  }   
 } 
} 

IMPORTANT: We are not presenting the other 3 usual files of the application.

Pay attention how we are doing a conversion (as ListBoxItem). This works for some types.

You can run the application. Please, you need to liberate the firewall.

After this introduction, some properties:

Background

The value will be a color.

BorderBrush

Also a color.

BorderThickness

FontFamily

FontSize

FontStyle

FontWeight

Foreground

The color of the text.

Height

Items

Very important. Is the collection of data. You need to use the method Add to include a new ListBoxItem. Something like in the sample:

  ...
  ListBoxItem li  = new ListBoxItem();
  li.Content = "Shoe" ;
  TheLB.Items.Add(li);
  ...

Margin

Name

Opacity

There are some properties for the capture of the selection:


 
SelectedIndex:   Gets or sets the index of the first item in the current selection or returns 
                    negative one (-1) if the selection is empty.  
SelectedItem:    Gets or sets the first item in the current selection or returns null if the selection is empty.  
SelectedItems:   Gets the currently selected items.  

SelectionMode

Can be:

Visibility

Width

Some events. To be overrided.

The more important (you can see how to use it at the sample):

SelectionChanged

KeyDown

KeyUp

And the usual mouse events:


  MouseDoubleClick    
  MouseDown    
  MouseEnter    
  MouseLeave    
  MouseLeftButtonDown    
  MouseLeftButtonUp    
  MouseMove    
  MouseRightButtonDown   
  MouseRightButtonUp    
  MouseUp    
   



PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE