View and Select Items Using a List : ListBox « Windows Presentation Foundation « C# / C Sharp






View and Select Items Using a List

View and Select Items Using a List
  

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

   
    
  








Related examples in the same category

1.ListBox With Items PanelListBox With Items Panel
2.Rotate a ListBoxRotate a ListBox
3.Scale a ListBoxScale a ListBox
4.Skew Transforms for a ListBoxSkew Transforms for a ListBox
5.ListBoxItem ContentListBoxItem Content
6.ListBox with ImageListBox with Image
7.ListBox with different font for each ListBoxItemListBox with different font for each ListBoxItem
8.Binding ListBox ItemsSource to Fonts.SystemFontFamiliesBinding ListBox ItemsSource to Fonts.SystemFontFamilies
9.ListBox and ListBox.ItemsListBox and ListBox.Items
10.ListBox Leveraging Content PropertyListBox Leveraging Content Property
11.ListBox and SelectionModeListBox and SelectionMode
12.ListBox Selected Index, Item, ValueListBox Selected Index, Item, Value
13.Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverterConvert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter
14.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.
15.ListBox SelectionChanged EventListBox SelectionChanged Event
16.ListBox SelectionMode=SingleListBox SelectionMode=Single
17.Get Selected Item from ListBoxGet Selected Item from ListBox
18.Display Bounded objects onto ListBoxDisplay Bounded objects onto ListBox
19.Use DataTemplate in ListBoxUse DataTemplate in ListBox
20.Load the Items in a ListBox AsynchronouslyLoad the Items in a ListBox Asynchronously
21.ListBox binds to the people collection, and sets the DataTemplate to use for displaying each item
22.Without specifying a DataTemplate, the ListBox displays a list of names.Without specifying a DataTemplate, the ListBox displays a list of names.
23.ListBox selection changed eventListBox selection changed event
24.Different Font Family and Size for each ListBoxItemDifferent Font Family and Size for each ListBoxItem
25.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.
26.Use Panel as a ListBoxItemUse Panel as a ListBoxItem
27.Create a ListBoxItem, set font, content, add the ListBoxItem to the ListBoxCreate a ListBoxItem, set font, content, add the ListBoxItem to the ListBox
28.Select All ListBox ItemsSelect All ListBox Items
29.Create a style that will produce a horizontal ListBox.Create a style that will produce a horizontal ListBox.
30.Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverterConvert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
31.Convert contents of a ListBoxItem to an instance of Thickness by using the BrushConverterConvert contents of a ListBoxItem to an instance of Thickness by using the BrushConverter
32.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
33.Add selected file to ListBoxAdd selected file to ListBox
34.ListBox with Image itemListBox with Image item
35.Fill up the ListBox with brush namesFill up the ListBox with brush names
36.Get selected item from ListBoxGet selected item from ListBox
37.List box with text and non-text content in the list box items.List box with text and non-text content in the list box items.
38.This list box allows multiple user selections.This list box allows multiple user selections.
39.List box that uses data binding to populate the list box items.List box that uses data binding to populate the list box items.
40.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.
41.Select All and unselect allSelect All and unselect all
42.Get selected item count from ListBoxGet selected item count from ListBox
43.Change the Appearance of Alternate Items in a ListChange the Appearance of Alternate Items in a List
44.Change the Appearance of a List Item When It's SelectedChange the Appearance of a List Item When It's Selected
45.Select Product Page FunctionSelect Product Page Function
46.Set text to TextBlock for selected list itemSet text to TextBlock for selected list item
47.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
48.Ensure there is at least one item selectedEnsure there is at least one item selected