Create a style that will produce a horizontal ListBox. : ListBox Style « 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 Style 
24.33.5.Create a style that will produce a horizontal ListBox.
<Window Background="cornsilk"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ListBox_Index.Window1">

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
      <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
      <Style TargetType="Separator">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type Separator}">
              <Border Width="2" Height="12" Margin="4" Background="Gray"/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

      <Style TargetType="ListBox">
        <Setter Property="ItemsPanel">
          <Setter.Value>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
          </Setter.Value>
        </Setter>
      </Style>
      
    </Grid.Resources>

    <ListBox Name="lb" Margin="10" Height="50" Grid.Column="0" Grid.Row="2" Grid.RowSpan="2" SelectionChanged="PrintText">
      <ListBoxItem>Item 1</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 2</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 3</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 4</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 5</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 6</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 7</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 8</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 9</ListBoxItem>
      <Separator/>
      <ListBoxItem>Item 10</ListBoxItem>
    </ListBox>
    <Label Margin="10, 10, 3, 3" Name="label1" Grid.Column="0" Grid.Row="5"/>


  </Grid>
</Window>
//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;

namespace ListBox_Index
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }  
        private void PrintText(object sender, SelectionChangedEventArgs args){
            ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
            iflbi == null return;
            label1.Content = "You chose " + lbi.Content.ToString() "."
        
    }
}
WPF Create A Style That Will Produce A Horizontal List Box
24.33.ListBox Style
24.33.1.Rotate a ListBoxRotate a ListBox
24.33.2.Scale a ListBoxScale a ListBox
24.33.3.Skew Transforms for a ListBoxSkew Transforms for a ListBox
24.33.4.Different Font Family and Size for each ListBoxItemDifferent Font Family and Size for each ListBoxItem
24.33.5.Create a style that will produce a horizontal ListBox.Create a style that will produce a horizontal ListBox.
24.33.6.Change the Appearance of Alternate Items in a ListChange the Appearance of Alternate Items in a List
24.33.7.Change the Appearance of a List Item When It's SelectedChange the Appearance of a List Item When It's 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.