IT tutorials
 
Mobile
 

Windows Phone 8 : Items Controls - ListBox, ComboBox

1/18/2015 8:16:25 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

Just as the ContentControl plays host to a single child control, the ItemsControl can have many children. Controls that can contain more than one child usually derive from the ItemsControl class; these include the ComboBox control and, most notably, the ListBox control (see Figure 1).

Image

FIGURE 1 ComboBox and ListBox are derived from ItemsControl.

An ItemsControl allows you to populate its children either directly in XAML or in code using the ItemsControl.Items property, or indirectly using a data binding and the ItemsControl.ItemsSource property.

The Selector class extends the ItemsControl to provide the properties and events shown in Table 1.

TABLE 1. Selector Members

Image

ListBox

The ListBox is an items control, which means that you can populate it with items that contain text or other controls.

By default, the ListBox control allows the user to select a single item in the list. Multiple items can be selected if the ListBox.SelectionMode property is set to Multiple (see the previous section on the CheckBox control, where a ListBox was used to display a data bound collection).

The ListBox control contains a list of ListBoxItem controls. The ListBox control is a remarkably flexible control because it allows you to not only display ListBoxItems, but because the ListBoxItem control is a ContentControl any kind of object can be placed in a ListBox (see Figure 2).

Image

FIGURE 2 ListBoxItem and ComboBoxItem are derived from ContentControl.

In addition, it is unnecessary to wrap content in a ListBoxItem control, as shown in the following excerpt:

<ListBox>
    <ListBoxItem>
        <TextBlock Text="Item 1" Style="{StaticResource NormalTextStyle}" />
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock Text="Item 2" Style="{StaticResource NormalTextStyle}" />
    </ListBoxItem>
</ListBox>

ListBoxItems are created implicitly for each element, making the previous excerpt equivalent to the following:

<ListBox>
    <TextBlock Text="Item 1" Style="{StaticResource NormalTextStyle}" />
    <TextBlock Text="Item 2" Style="{StaticResource NormalTextStyle}" />
</ListBox>

The example depicted in Figure 3 demonstrates that placing a TextBlock, implicitly or explicitly, in a ListBoxItem yields the same results.

Image

FIGURE 3 ListBox with implicit and explicit ListBoxItems.

This works because the ListBoxItem ControlTemplate uses a TemplateBinding to the Content and ContentTemplate properties of the item control. When an item is being displayed, the ControlTemplate places it in a ContentControl, as this excerpt from the built-in ListBoxItem ControlTemplate shows:

<ControlTemplate TargetType="ListBoxItem">
    <Border x:Name="LayoutRoot">
        <ContentControl x:Name="ContentContainer"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        Content="{TemplateBinding Content}" />
    </Border>
</ControlTemplate>

The ListBox control inherits the Selector.SelectedItem and SelectedIndex properties together with a SelectionChanged event, which allow you to detect when the user selects or deselects an item.


Note

The SelectedItem property is the value of the content item and not the ListBoxItem itself. It is the Content property of the ListBoxItem that is used to resolve the value.


For an example of how to consume the ItemsSource property using a data binding, see the previous sections on the CheckBox and RadioButton controls.

In addition to the selection related properties provided by the Selector class, the ListBox also provides a SelectedItems property, which can be used when using the multiple SelectionMode. The SelectedItems property is, however, not a DependencyProperty, and this means that it is not available for data binding. See the previous section on the CheckBox control for a solution.

ComboBox

Although the ComboBox control exists in the FCL, it is not suitable for use in most cases. The ComboBox has its origins in desktop UI and is more suited to a mouse-driven UI, where the user can rely on a high level of precision manipulating the control using a mouse.

For this reason, the ComboBox does not appear in the Visual Studio Toolbox, and the built-in styles for the ComboBox do not reflect the light and dark themes of the phone.


 
Others
 
- Windows Phone 8 : LinkedIn (part 3) - Posting a Message, The Feeds Filter
- Windows Phone 8 : LinkedIn (part 2) - Accessing LinkedIn Content, Responding to a LinkedIn Post
- Windows Phone 8 : LinkedIn (part 1) - Configuring Your LinkedIn Account
- Windows Phone 8 : Twitter (part 2) - Checking for Tweets, Responding to a Tweet, Checking Twitter Notifications
- Windows Phone 8 : Twitter (part 1) - Configuring Your Twitter Account
- Windows Phone 8 : Working with People - The Me Tile (part 4) - Chatting with Friends, Setting Your Chat Status, Notifications
- Windows Phone 8 : Working with People - The Me Tile (part 3) - Uploading a Photo
- Windows Phone 8 : Working with People - The Me Tile (part 2) - Posting Status Updates, Checking In
- Windows Phone 8 : Working with People - The Me Tile (part 1) - Viewing Your Own Social Networking Wall
- Windows Phone 8 : Receiving Input with Buttons - Check Box
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS