Learning Windows Phone7 development together (13th. 8 ListBox control)

Source: Internet
Author: User

Only the listbox control is removed from the listview control in Phone7. However, this control is very powerful and can fully implement the listview function. This control is equivalent to a container. You can use ListItem to combine multiple controls to obtain a list of different functions.

Xaml:

<ListBox Grid. row = "1" Height = "567" HorizontalAlignment = "Left" Margin = "460," Name = "listBox1" VerticalAlignment = "Top" Width = "">

<ListBox. ItemTemplate>

<DataTemplate>

<StackPanel Orientation = "Horizontal">

<Image Source = "{Binding ImgSource}" Width = "130" Height = "130"/>

<TextBlock Text = "{Binding Name}" Foreground = "Yellow" FontSize = "25"/>

</StackPanel>

</DataTemplate>

</ListBox. ItemTemplate>

</ListBox>

// {Binding ImgSource}: dynamically binds image resources.

// {Binding Name}: dynamically binds text.

Source code:

ImageList item1 = new ImageList ();

Item1.ImgSource = new BitmapImage (new Uri ("Images/Chrysanthemum.jpg", UriKind. Relative ));

Item1.Name = "Chrysanthemum.jpg ";

List. Add (item1 );

 

ImageList item2 = new ImageList ();

Item2.ImgSource = new BitmapImage (new Uri ("Images/Desert.jpg", UriKind. Relative ));

Item2.Name = "Desert.jpg ";

List. Add (item2 );

 

ImageList item3 = new ImageList ();

Item3.ImgSource = new BitmapImage (new Uri ("Images/Hydrangeas.jpg", UriKind. Relative ));

Item3.Name = "Hydrangeas.jpg ";

List. Add (item3 );

 

ListBox1.ItemsSource = list;

 

 

<ListBox Height = "605" HorizontalAlignment = "Left" Margin = "460," Name = "listBox1" verticalignment = "Top" Width = "">

<ListBox. ItemTemplate>

<DataTemplate>

<StackPanel Orientation = "Horizontal">

<Button Content = "{Binding ColorName}" Click = "Button_Click"/>

<TextBlock x: Name = "tb" Text = "{Binding ColorName}" FontSize = "35"/>

</StackPanel>

</DataTemplate>

</ListBox. ItemTemplate>

</ListBox>

 

Void MainPage_Loaded (object sender, RoutedEventArgs e)

{

ColorItem color = new ColorItem ();

Color. ColorName = "Red ";

Color. ColorValue = Colors. Red;

List. Add (color );

 

ColorItem color1 = new ColorItem ();

Color1.ColorName = "Green ";

Color1.ColorValue = Colors. Green;

List. Add (color1 );

 

ColorItem color2 = new ColorItem ();

Color2.ColorName = "Blue ";

Color2.ColorValue = Colors. Blue;

List. Add (color2 );

 

This. listBox1.ItemsSource = list;

}

 

Private void Button_Click (object sender, RoutedEventArgs e)

{

String button = (sender as Button). Content as string;

Int index = 0;

String color = string. Empty;

For (index = 0; index <list. Count; index ++)

{

If (button = list [index]. ColorName)

{

Color = list [index]. ColorName;

Break;

}

 

}

ListBoxItem lbi = this. listBox1.ItemContainerGenerator. ContainerFromIndex (index) as ListBoxItem;

TextBlock tb = FindFirstVisualChild <TextBlock> (lbi, "tb ");

Tb. Foreground = new SolidColorBrush (list [index]. ColorValue );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.