ListBox SelectionMode=Single : ListBox « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » ListBox 
24.29.3.ListBox SelectionMode=Single
<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() ".";
        }
    }
}
WPF List Box Selection Mode Single
24.29.ListBox
24.29.1.ListBox and SelectionModeListBox and SelectionMode
24.29.2.ListBox Selected Index, Item, ValueListBox Selected Index, Item, Value
24.29.3.ListBox SelectionMode=SingleListBox SelectionMode=Single
24.29.4.Get Selected Item from ListBoxGet Selected Item from ListBox
24.29.5.Load the Items in a ListBox AsynchronouslyLoad the Items in a ListBox Asynchronously
24.29.6.ListBox selection changed eventListBox selection changed event
24.29.7.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.
24.29.8.Select All ListBox ItemsSelect All ListBox Items
24.29.9.Add selected file to ListBoxAdd selected file to ListBox
24.29.10.Fill up the ListBox with brush namesFill up the ListBox with brush names
24.29.11.Get selected item from ListBoxGet selected item from ListBox
24.29.12.This list box allows multiple user selections.This list box allows multiple user selections.
24.29.13.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.
24.29.14.Select All and unselect allSelect All and unselect all
24.29.15.Get selected item count from ListBoxGet selected item count from ListBox
24.29.16.View and Select Items Using a ListView and Select Items Using a List
24.29.17.Set text to TextBlock for selected list itemSet text to TextBlock for selected list item
24.29.18.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
24.29.19.Ensure there is at least one item selectedEnsure there is at least one item selected
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.