Share via


IXRListBoxItem (Compact 2013)

3/28/2014

This class represents a selectable item in a list box.

Syntax

class IXRListBoxItem : public IXRContentControl

Inheritance Hierarchy

IXRDependencyObject

    IXRUIElement

        IXRFrameworkElement

            IXRControl

                IXRContentControl

                    IXRListBoxItem

Methods

Method

Description

IXRListBoxItem::GetIsSelected

Retrieves a value that indicates whether this list-box item is selected.

IXRListBoxItem::SetIsSelected

Sets a value that indicates whether this list-box item is selected.

Thread Safety

Members of this class are thread-safe if you previously called IXRApplication::CreateHostFromXaml and supplied it with an XRWindowCreateParams structure that has AllowsMultipleThreadAccess set to true.

Remarks

An IXRListBox object can contain a collection of IXRListBoxItem objects. This collection is stored in an IXRItemCollection object, which can be retrieved by calling IXRItemsControl::GetItems. To add IXRListBoxItem items to the retrieved IXRItemCollection object, you can use the IXRItemCollection::Add or IXRItemCollection::Insert method.

IXRListBoxItem inherits from IXRContentControl. You can retrieve the content of this list-box item by calling IXRContentControl::GetContent, and you can set the content of this list-box item by calling IXRContentControl::SetContent.

A list box can also contain objects other than IXRListBoxItem objects.

The objects retain their original types. For example, if the list is filled with images then IXRListBox::GetSelectedItems returns a list of images, and if the list is filled with text blocks then GetSelectedItems returns a list of IXRTextBlock objects.

To enable each list-box item to respond to user interaction, you can add event handlers such as MouseEnter or MouseLeftButtonDown to its events. You do this by calling the inherited methods IXRUIElement::AddMouseEnterEventHandler or IXRUIElement::AddMouseLeftButtonDownEventHandler. To create a visual behavior for each list-box item when the list box is loaded, you can start a storyboard animation in an event handler, and then add its delegate to a list-box item by calling IXRFrameworkElement::AddLoadedEventHandler.

You can define a list box and list-box items in Microsoft Silverlight 3 XAML. For information about the differences between XAML in XAML for Windows Embedded and Silverlight 3, see Differences Between Microsoft Silverlight 3 and XAML for Windows Embedded. For more information about how to define this element in the source XAML for your application, see the ListBox Class and ListBoxItem Class on MSDN.

When you create a class instance, use an IXRListBoxItemPtr smart pointer instead of a raw interface pointer. For more information, see XRPtr<Interface>.

Example

The following code example creates an IXRListBoxItem object that has an image brush as its background. It also attaches delegates to represent event handlers that are already implemented in CustomObject, a custom object. The event handlers enable the object to respond to events, such as when the user presses the left mouse button while it is resting over the list-box item.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "windows.h"
#include "XamlRuntime.h"
#include "XRDelegate.h"
#include "XRPtr.h"

void CreateListBoxItem(IXRApplication* pApplication, IXRListBox* pListBox, CustomObject* pObject)
{
     // Initialize variables

     IXRListBoxItemPtr pLocationBasedItem;
     IXRImageBrushPtr pImageBrush;
     IXRBitmapImagePtr pDinerBitmap;

     float itemHeight = 50;
     float itemWidth = 100;
     XRValue itemValue;
     itemValue.vType = VTYPE_READONLY_STRING;
     itemValue.pReadOnlyStringVal = L"Ninth Avenue Diner";

     // Create new XR objects

     pApplication->CreateObject(&pLocationBasedItem);
     pApplication->CreateObject(&pImageBrush);
     pApplication->CreateObject(&pDinerBitmap);

     // Set values for the image brush to paint the list-box item

     pDinerBitmap->SetUriSource(L"Assets/ninthAve.png");
     pImageBrush->SetImageSource((IXRImageSource*)&pDinerBitmap);

     // Set values for the list-box item

     pLocationBasedItem->AddMouseDownEventHandler(CreateDelegate(pObject, &CustomObject::OnMouseDown));
     pLocationBasedItem->AddMouseEnterEventHandler(CreateDelegate(pObject, &CustomObject::OnMouseEnter));
     pLocationBasedItem->AddMouseLeaveEventHandler(CreateDelegate(pObject, &CustomObject::OnMouseLeave));
     pLocationBasedItem->AddOnLoadedEventHandler(CreateDelegate(pObject, &CustomObject::OnLoaded));
     pLocationBasedItem->SetHeight(itemHeight);
     pLocationBasedItem->SetWidth(itemWidth);
     pLocationBasedItem->SetContent(&itemValue);
     pLocationBasedItem->SetBackground((IXRBrush*)&pImageBrush);

     // Add the new list-box item to the item collection

     IXRItemCollectionPtr pItemCollection;
     UINT index = 0;
     XRValue xrValue;
     xrValue.vType = VTYPE_OBJECT;
     xrValue.pObjectVal = pLocationBasedItem;
     pListBox->GetItems(&pItemCollection);
     pItemCollection->Insert(index, &xrValue);

}

This code example assumes that you already created an IXRApplication instance and generated a visual tree, and that an IXRListBox object already exists to accommodate this list-box item. It also assumes that you already implemented event-handling code for the four list-box event handlers in a custom object named CustomObject. For more information about the classes used in this code example, see IXRListBox, IXRItemCollection, IXRImageBrush, and IXRBitmapImage.

.NET Framework Equivalent

System.Windows.Controls.ListBoxItem

Requirements

Header

XamlRuntime.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

Classes for UI Element Management