.NET Framework Class Library for Silverlight
DataGrid..::.ItemsSource Property

Gets or sets a collection that is used to generate the content of the control.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)
Syntax

Visual Basic (Declaration)
Public Property ItemsSource As IEnumerable
    Get
    Set
Visual Basic (Usage)
Dim instance As DataGrid
Dim value As IEnumerable

value = instance.ItemsSource

instance.ItemsSource = value
C#
public IEnumerable ItemsSource { get; set; }
XAML Attribute Usage
<data:DataGrid ItemsSource="bindingDeclaration"/>
- or -
<data:DataGrid ItemsSource="resourceReferenceToIEnumerable"/>

XAML Values

data:

A prefix that is defined to map the XML namespace for the System.Windows.Controls.Data assembly and the System.Windows.Controls CLR namespace.

bindingDeclaration

A Binding declaration using a {Binding ....} markup extension. For more information, see Binding Markup Extension or Binding.

resourceReferenceToIEnumerable

A resource reference to an existing object of type IEnumerable from a resources collection. The resource reference must specify the desired IEnumerable by key.

Property Value

Type: System.Collections..::.IEnumerable
The collection that is used to generate the content of the control. The default is nullNothingnullptra null reference (Nothing in Visual Basic).
Examples

The following code example demonstrates how to set the AutoGenerateColumns and ItemsSource properties. This example is part of a larger example available in the DataGrid class overview.

XAML
<data:DataGrid x:Name="dataGrid1" 
    Height="140" Margin="0,5,0,10"
    AutoGenerateColumns="True" />            
Visual Basic
dataGrid1.ItemsSource = Customer.GetSampleCustomerList()
C#
// Set the ItemsSource to autogenerate the columns.
dataGrid1.ItemsSource = Customer.GetSampleCustomerList();
Visual Basic
Public Class Customer
    Dim m_FirstName As String
    Dim m_LastName As String
    Dim m_Address As String
    Dim m_IsNew As Boolean
    Dim m_IsSubscribed As Boolean?

    Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal address As String, ByVal isNew As Boolean, ByVal isSubscribed As Boolean?)
        Me.FirstName = firstName
        Me.LastName = lastName
        Me.Address = address
        Me.IsNew = isNew
        Me.IsSubscribed = isSubscribed
    End Sub

    Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(ByVal value As String)
            m_FirstName = value
        End Set
    End Property

    Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(ByVal value As String)
            m_LastName = value
        End Set
    End Property

    Property Address() As String
        Get
            Return m_Address
        End Get
        Set(ByVal value As String)
            m_Address = value
        End Set
    End Property

    Property IsNew() As Boolean
        Get
            Return m_IsNew
        End Get
        Set(ByVal value As Boolean)
            m_IsNew = value
        End Set
    End Property

    Property IsSubscribed() As Boolean?
        Get
            Return m_IsSubscribed
        End Get
        Set(ByVal value As Boolean?)
            m_IsSubscribed = value
        End Set
    End Property

    Public Shared Function GetSampleCustomerList() As List(Of Customer)
        Return New List(Of Customer)(New Customer() _
               {New Customer("A.", "Zero", "12 North Third Street, Apartment 45", False, True), _
                New Customer("B.", "One", "34 West Fifth Street, Apartment 67", False, False), _
                New Customer("C.", "Two", "56 East Seventh Street, Apartment 89", True, Nothing), _
                New Customer("D.", "Three", "78 South Ninth Street, Apartment 10", True, True)})
    End Function
End Class
C#
public class Customer
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }
    public Boolean IsNew { get; set; }

    // A null value for IsSubscribed can indicate 
    // "no preference" or "no response".
    public Boolean? IsSubscribed { get; set; }

    public Customer(String firstName, String lastName, 
        String address, Boolean isNew, Boolean? isSubscribed)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
        this.IsNew = isNew; 
        this.IsSubscribed = isSubscribed;
    }

    public static List<Customer> GetSampleCustomerList()
    {
        return new List<Customer>(new Customer[4] {
            new Customer("A.", "Zero", 
                "12 North Third Street, Apartment 45", 
                false, true), 
            new Customer("B.", "One", 
                "34 West Fifth Street, Apartment 67", 
                false, false),
            new Customer("C.", "Two", 
                "56 East Seventh Street, Apartment 89", 
                true, null),
            new Customer("D.", "Three", 
                "78 South Ninth Street, Apartment 10", 
                true, true)
        });
    }
}
Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags :


Page view tracker