|
|
Questions and answers for Controls ListBox.
|
64. Controls ListBox
|
| 64.1 How do I bind a
string array with a ListBox?
|
| 64.2 How do I display
items in a ListBox horizontally?
|
64.1 How do I bind a string array with a ListBox?
|
|
A ListBox can be bound with a string array using the ItemsSource property of the
ListBox control. The following code snippet is used to bind a string array with
the ListBox.
|
[XAML]
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Loaded="Window_Loaded">
<Grid>
<ListBox Name="LB2"/>
</Grid>
</Window>
|
|
|
|
[C#]
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] lbitems = { "Item1", "Item2", "Item3", "Item4" };
LB2.ItemsSource = lbitems;
}
|
64.2 How do I display items in a ListBox horizontally?
|
|
Items can be displayed horizontally using the ItemPanel property of the ListBox.
The following lines of code are used to display the items horizontally.
|
[XAML]
<ListBox Height="40" Width="50">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>1 ITEM 1</ListBoxItem>
<ListBoxItem>2 ITEM 2</ListBoxItem>
<ListBoxItem>3 ITEM 3</ListBoxItem>
</ListBox>
|
|
|