RadControls for Silverlight

Usually the data source to which you are binding your controls will contain not just simple primitive types, but also data objects. By default the items’ text will be the result of the ToString() method of each data object. To display the value of a property of the data object you have to set the DisplayMemberPath property, or the ItemTemplate property of the control. This topic will cover both scenarios:

Consider the following situation. You have a ListBox which is populated with a collection of business objects.

CopyXAML
<ListBox x:Name="listBox"/>
CopyC#
this.listBox.ItemsSource = this.CreateSampleData();
CopyVB.NET
Me.listBox.ItemsSource = Me.CreateSampleData()

In this example the following business object is used:

CopyC#
public class MyData
{
    public MyData( string color )
    {
        this.Color = color;
    }
    public string Color
    {
        get;
        set;
    }
}
CopyVB.NET
Public Class MyData
    Public Sub New(ByVal color As String)
        Me.Color = color
    End Sub

Private _Color As String
    Public Property Color() As String
        Get
            Return _Color
        End Get
        Set(ByVal value As String)
            _Color = value
        End Set
    End Property
End Class

Without specifying neither the DisplayMemberPath nor the ItemTemplate, the result will be similar to the one on the snapshot below.

 
Common Data Binding Data Template 010

The ListBox doesn't know what to do and how to represent the passed business objects.

Using DisplayMemberPath

The following example demonstrates you the usage of the DisplayMemberPath property.

CopyXAML
<ListBox x:Name="listBox" DisplayMemberPath="Color"/>

And the result will be correctly displayed business objects.

 
Common Data Binding Data Template 020

Using DataTemplate

Another way to specify which properties should be displayed is to create a DataTemplate. The next example demonstrates how to use the ItemTemplate property of the ListBox:

CopyXAML
<ListBox x:Name="listBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Color}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

See Also