共用方式為


Binding Silverlight Controls to Data in Visual Studio

To create data-bound controls, you can drag items from the Data Sources window to the Silverlight Designer.

For general information about creating data-bound controls in Visual Studio, see Binding Controls to Data in Visual Studio. For more information about data binding with Silverlight, see Data Binding.

Generated XAML and Code

When you drag an item from the Data Sources window to the Silverlight designer, Visual Studio generates XAML that defines a new data-bound control (or binds an existing control to the data source).

When you drag items from the Data Sources window to the designer, Visual Studio generates XAML that does the following:

  • Adds a new CollectionViewSource to the pages resources that can be used to navigate and bind to the data.

  • If you drop the item into an empty area of the grid, a new control is created that is data bound to that item.

  • If you drop the item onto an existing control, that control is configured as data bound to that item.

Visual Studio also makes the following changes to the code-behind file:

  • Creates a Loaded event handler and includes the following helper comment. Uncomment the code and replace the 'Resource Key for CollectionViewSource' with the name of the generated CollectionViewSource. Also replace 'your data' with a pointer to the collection of your custom business objects. For an example that demonstrates how to modify this comment, see Walkthrough: Binding Silverlight Controls to Objects.

    // Do not load your data at design time.
    // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    // {
    //    //Load your data here and assign the result to the CollectionViewSource.
    //    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
    //     myCollectionViewSource.Source = your data
    // }
    
    'Do not load your data at design time.
    'If Not (System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me)) Then
    '    'Load your data here and assign the result to the CollectionViewSource.
    '    Dim myCollectionViewSource As System.Windows.Data.CollectionViewSource = CType(Me.Resources("Resource Key for CollectionViewSource"), System.Windows.Data.CollectionViewSource)
    '    myCollectionViewSource.Source = your data
    'End If
    

Services

When you drag a service object or property from the Data Sources window to the designer, Visual Studio generates XAML that creates a data-bound control (or binds an existing control to the object or property). However, Visual Studio does not generate code that fills the proxy service object with data. You must write this code yourself. For an example that demonstrates how to do this, see Walkthrough: Binding Silverlight Controls to a WCF Data Service.

Objects

When you drag an object or property from the Data Sources window to the designer, Visual Studio generates XAML that creates a data-bound control (or binds an existing control to the object or property). However, Visual Studio does not generate code to fill the object with data. You must write this code yourself. For an example that demonstrates how to do this, see Walkthrough: Binding Silverlight Controls to Objects.

The following table lists topics that show how to create data bound controls in Silverlight applications by dragging items from the Data Sources window.

Task

More information

Create controls bound to data returned by a service.

Walkthrough: Binding Silverlight Controls to a WCF Data Service

Create controls bound to data in objects.

Walkthrough: Binding Silverlight Controls to Objects

See Also

Other Resources

Silverlight

Data Access and Data Structures