Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
WPF ListBox

WPF ListBox

Ratings: (0)|Views: 0|Likes:
Published by Abhi

More info:

Published by: Abhi on Mar 12, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/13/2014

pdf

text

original

 
WPF ListBox
Introduction
 The ListBox tag represents a Silverlight ListBox control in XAML.
<ListBox></ListBox>
 
 The Width and Height properties represent the width and the height of aListBox. The Name property represents the name of the control, which is aunique identifier of a control. The Margin property tells the location of aListBox on the parent control. The HorizontalAlignment and VerticalAlignmentproperties are used to set horizontal and vertical alignments. The following code snippet sets the name, height, and width of a ListBoxcontrol. The code also sets horizontal alignment to left and vertical alignmentto top.
<ListBoxMargin="10,10,0,13"Name="listBox1"HorizontalAlignment="Left"  VerticalAlignment="Top"Width="194"Height="200" />
 
Adding ListBox Items
 
A ListBox control hosts a collection of ListBoxItem. The following code snippetadds items to a ListBox control.
<ListBoxMargin="10,10,0,13"Name="listBox1"HorizontalAlignment="Left"  VerticalAlignment="Top"Width="194"Height="200">
 
<ListBoxItemContent="Coffie"></ListBoxItem>
 
<ListBoxItemContent="Tea"></ListBoxItem>
 
<ListBoxItemContent="Orange Juice"></ListBoxItem>
 
<ListBoxItemContent="Milk"></ListBoxItem>
 
<ListBoxItemContent="Iced Tea"></ListBoxItem>
 
<ListBoxItemContent="Mango Shake"></ListBoxItem>
 
</ListBox>
 
 The above code generates Figure 1.
 
 
Figure 1. ListBox with items
Adding ListBox Items Dynamically
 
In the previous section, we saw how to add items to a ListBox at design-timefrom XAML. We can add items to a ListBox from the code.Let's change our UI and add a TextBox and a button control to the page. TheXAML code for the TextBox and Button controls look like following:
 
<TextBoxHeight="23"HorizontalAlignment="Left"Margin="8,14,0,0"  Name="textBox1"VerticalAlignment="Top"Width="127" />
 
<ButtonHeight="23"Margin="140,14,0,0"Name="button1"VerticalAlignment="Top" HorizontalAlignment="Left"Width="76"Click="button1_Click">
 
Add Item
 
</Button>
 
 The final UI looks like Figure 2.
 
 
Figure 2.
 
On button click event handler, we add the content of TextBox to the ListBox bycalling ListBox.Items.Add method. The following code adds TextBox contentsto the ListBox items.
private voidbutton1_Click(objectsender,RoutedEventArgse)
 
{
 
listBox1.Items.Add(textBox1.Text);
 
}
 
On button click event handler, we add the content of TextBox to the ListBox bycalling ListBox.Items.Add method.Now if you enter text in the TextBox and click Add Item button, it will addcontents of the TextBox to the ListBox.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->