1 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 x:Class="ListBoxStyle.Page1"
6 <TabControl MinHeight="500" MaxHeight="700" MinWidth="400">
7 <TabItem Style="{StaticResource TabStyle}" Header="List Box Sample" IsSelected="true">
9 <TextBlock Style="{StaticResource HeaderStyle}">List Box Styles</TextBlock>
11 <TextBlock Style="{StaticResource mainContentStyle}">This example demonstrates ListBox controls with styles applied.</TextBlock>
16 <TabItem Name="xaml" Style="{StaticResource TabStyle}" Header="XAML">
21 <TabItem Name="xamlcsharp" Style="{StaticResource TabStyle}" Header="XAML + C#">
22 <TabControl TabStripPlacement="Right">
23 <TabItem Name="xcsharpCheck" Style="{StaticResource TabStyle2}" Header="XAML">
24 <ScrollViewer HorizontalScrollBarVisibility="Visible">
25 <TextBox Style="{StaticResource CodeSnippetParagraph}" xml:space="preserve">
29 <Style x:Key="Simple" TargetType="{x:Type ListBox}">
30 <Setter Property = "SelectionMode" Value="Single"/>
31 <Setter Property = "Background" Value="Red"/>
34 <Style x:Key="SimpleListBoxItem" TargetType="{x:Type ListBoxItem}">
35 <Setter Property = "FontSize" Value="14"/>
36 <Setter Property = "Background" Value="Pink"/>
37 <Setter Property = "Foreground" Value="Purple"/>
40 <Style x:Key="Triggers" TargetType="{x:Type ListBoxItem}">
42 <Trigger Property="ListBoxItem.IsMouseOver" Value="true">
43 <Setter Property = "Foreground" Value="Red"/>
44 <Setter Property = "Background" Value="LightBlue"/>
48 </Canvas.Resources>
51 <Grid Name="grid" ShowGridLines ="false" Background ="White" >
52 <Grid.ColumnDefinitions>
53 <ColumnDefinition Width="300"/>
54 <ColumnDefinition Width="250"/>
55 </Grid.ColumnDefinitions>
56 <Grid.RowDefinitions>
70 </Grid.RowDefinitions>
71 <TextBlock Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="0" FontSize="24">List Boxes</TextBlock>
72 <TextBlock Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="1" FontSize="14" Grid.ColumnSpan="2">The following list box examples are created using styles:</TextBlock>
74 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="2">
75 <TextBox Name="tb" Width="140" Height="30"></TextBox>
77 <ListBox Style="{StaticResource Simple}" Width="100" Height="55" SelectionChanged="PrintText">
79 <ListBoxItem>Item 1</ListBoxItem>
80 <ListBoxItem>Item 2</ListBoxItem>
81 <ListBoxItem>Item 3</ListBoxItem>
82 <ListBoxItem>Item 4</ListBoxItem>
83 <ListBoxItem>Item 5</ListBoxItem>
84 <ListBoxItem>Item 6</ListBoxItem>
85 <ListBoxItem>Item 7</ListBoxItem>
86 <ListBoxItem>Item 8</ListBoxItem>
87 <ListBoxItem>Item 9</ListBoxItem>
88 <ListBoxItem>Item 10</ListBoxItem>
91 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="2" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style sets the list box to only allow one item to be selected at a time and changes the background color.</TextBlock>
93 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="3">
94 <ListBox Width="260" Height="55" HorizontalAlignment="Left">
96 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 1</ListBoxItem>
97 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 2</ListBoxItem>
98 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 3</ListBoxItem>
99 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 4</ListBoxItem>
100 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 5</ListBoxItem>
101 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 6</ListBoxItem>
102 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 7</ListBoxItem>
103 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 8</ListBoxItem>
104 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 9</ListBoxItem>
105 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 10</ListBoxItem>
108 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="3" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style changes the background, foreground, and font size of the list box items.</TextBlock>
110 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="4">
111 <ListBox Width="260" Height="55" HorizontalAlignment="Left">
113 <ListBoxItem Style="{StaticResource Triggers}">Item 1</ListBoxItem>
114 <ListBoxItem Style="{StaticResource Triggers}">Item 2</ListBoxItem>
115 <ListBoxItem Style="{StaticResource Triggers}">Item 3</ListBoxItem>
116 <ListBoxItem Style="{StaticResource Triggers}">Item 4</ListBoxItem>
117 <ListBoxItem Style="{StaticResource Triggers}">Item 5</ListBoxItem>
118 <ListBoxItem Style="{StaticResource Triggers}">Item 6</ListBoxItem>
119 <ListBoxItem Style="{StaticResource Triggers}">Item 7</ListBoxItem>
120 <ListBoxItem Style="{StaticResource Triggers}">Item 8</ListBoxItem>
121 <ListBoxItem Style="{StaticResource Triggers}">Item 9</ListBoxItem>
122 <ListBoxItem Style="{StaticResource Triggers}">Item 10</ListBoxItem>
125 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="4" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style changes the background and foreground colors of the list box items in response to a mouse over event.</TextBlock>
133 <TabItem Style="{StaticResource TabStyle2}" Header="C#">
134 <ScrollViewer HorizontalScrollBarVisibility="Visible">
135 <TextBox Style="{StaticResource CodeSnippetParagraph}" xml:space="preserve">
138 using System.Windows;
139 using System.Windows.Controls;
140 using System.Windows.Documents;
141 using System.Windows.Navigation;
142 using System.Windows.Shapes;
143 using System.Windows.Data;
145 namespace ListBoxEvent_wcp
147 /// Interaction logic for Pane1.xaml
149 public partial class Pane1 : Canvas
151 void PrintText(object sender, RoutedEventArgs args)
153 ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
155 tb.Text = " You selected " + li.Content.ToString() + ".";
165 <TabItem Name="xamlvb" Style="{StaticResource TabStyle}" Header="XAML + Visual Basic.NET">
166 <TabControl TabStripPlacement="Right">
167 <TabItem Name="xvbCheck" Style="{StaticResource TabStyle2}" Header="XAML"></TabItem>
168 <TabItem Style="{StaticResource TabStyle2}" Header="VB.NET"></TabItem>
172 <TabItem Name="csharp" Style="{StaticResource TabStyle}" Header="C#"></TabItem>
174 <TabItem Name="vb" Style="{StaticResource TabStyle}" Header="Visual Basic.NET"></TabItem>
176 <TabItem Name="managedcpp" Style="{StaticResource TabStyle}" Header="Managed C++"></TabItem>
178 <TabItem Style="{StaticResource TabStyle}" Header="Preview Sample">
184 <Style x:Key="Simple" TargetType="{x:Type ListBox}">
185 <Setter Property = "SelectionMode" Value="Single"/>
186 <Setter Property = "Background" Value="Red"/>
189 <Style x:Key="SimpleListBoxItem" TargetType="{x:Type ListBoxItem}">
190 <Setter Property = "FontSize" Value="14"/>
191 <Setter Property = "Background" Value="Pink"/>
192 <Setter Property = "Foreground" Value="Purple"/>
195 <Style x:Key="Triggers" TargetType="{x:Type ListBoxItem}">
197 <Trigger Property="ListBoxItem.IsMouseOver" Value="true">
198 <Setter Property = "Foreground" Value="Red"/>
199 <Setter Property = "Background" Value="LightBlue"/>
206 <Grid Name="grid" ShowGridLines ="false" Background ="White" >
207 <Grid.ColumnDefinitions>
208 <ColumnDefinition Width="300"/>
209 <ColumnDefinition Width="250"/>
210 </Grid.ColumnDefinitions>
211 <Grid.RowDefinitions>
225 </Grid.RowDefinitions>
226 <TextBlock Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="0" FontSize="24">List Boxes</TextBlock>
227 <TextBlock Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="1" FontSize="14" Grid.ColumnSpan="2">The following list box examples are created using styles:</TextBlock>
228 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="2">
230 <TextBox Name="tb" Width="140" Height="30"></TextBox>
232 <ListBox Style="{StaticResource Simple}" Width="100" Height="55" SelectionChanged="PrintText">
234 <ListBoxItem>Item 1</ListBoxItem>
235 <ListBoxItem>Item 2</ListBoxItem>
236 <ListBoxItem>Item 3</ListBoxItem>
237 <ListBoxItem>Item 4</ListBoxItem>
238 <ListBoxItem>Item 5</ListBoxItem>
239 <ListBoxItem>Item 6</ListBoxItem>
240 <ListBoxItem>Item 7</ListBoxItem>
241 <ListBoxItem>Item 8</ListBoxItem>
242 <ListBoxItem>Item 9</ListBoxItem>
243 <ListBoxItem>Item 10</ListBoxItem>
246 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="2" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style sets the list box to only allow one item to be selected at a time and changes the background color.</TextBlock>
248 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="3">
249 <ListBox Width="260" Height="55" HorizontalAlignment="Left">
251 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 1</ListBoxItem>
252 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 2</ListBoxItem>
253 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 3</ListBoxItem>
254 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 4</ListBoxItem>
255 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 5</ListBoxItem>
256 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 6</ListBoxItem>
257 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 7</ListBoxItem>
258 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 8</ListBoxItem>
259 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 9</ListBoxItem>
260 <ListBoxItem Style="{StaticResource SimpleListBoxItem}">Item 10</ListBoxItem>
263 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="3" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style changes the background, foreground, and font size of the list box items.</TextBlock>
265 <DockPanel Margin="10, 10, 3, 3" Grid.Column="0" Grid.Row="4">
266 <ListBox Width="260" Height="55" HorizontalAlignment="Left">
268 <ListBoxItem Style="{StaticResource Triggers}">Item 1</ListBoxItem>
269 <ListBoxItem Style="{StaticResource Triggers}">Item 2</ListBoxItem>
270 <ListBoxItem Style="{StaticResource Triggers}">Item 3</ListBoxItem>
271 <ListBoxItem Style="{StaticResource Triggers}">Item 4</ListBoxItem>
272 <ListBoxItem Style="{StaticResource Triggers}">Item 5</ListBoxItem>
273 <ListBoxItem Style="{StaticResource Triggers}">Item 6</ListBoxItem>
274 <ListBoxItem Style="{StaticResource Triggers}">Item 7</ListBoxItem>
275 <ListBoxItem Style="{StaticResource Triggers}">Item 8</ListBoxItem>
276 <ListBoxItem Style="{StaticResource Triggers}">Item 9</ListBoxItem>
277 <ListBoxItem Style="{StaticResource Triggers}">Item 10</ListBoxItem>
280 <TextBlock Margin="10, 10, 3, 3" Grid.Column="1" Grid.Row="4" FontSize="12" VerticalAlignment="Center" TextWrapping="Wrap">Style changes the background and foreground colors of the list box items in response to a mouse over event.</TextBlock>