Binding Objects in an Object Collection
In This Topic
The C1CoverFlow control can be bound to a collection of items. In this section, you will bind the control to an ObservableCollection of type string. The collection you will bind to will be passed as a data context. This topic assumes you are working in Microsoft Expression Blend and that you have added a C1CoverFlow control to your project.
Complete the following steps:
- Select the C1CoverFlow control and set the following properties:
- Set the Width property to “400”.
- Set the Height property to “200”.
- Set the SelectedIndex property to “2”.
- Add images to the project by completing the following steps:
- In the Projects panel, right-click the project to open its context menu and select Add New Folder; name the new folder “Images”.
- Right-click the Images folder and select Add Existing Item. The Add Existing Item dialog box opens.
- Navigate to the following location:
- In XP: C:\Documents and Settings\<username>\My Documents\ComponentOne Samples\Studio for Silverlight\QuickStart\QuickStart
- Windows7/Vista: C:\Users\<username>\Documents\ComponentOne Samples\Studio for Silverlight\QuickStart\QuickStart
- Select cover1.jpg, cover2.jpg, cover3.jpg, cover4.jpg, and cover5.jpg.
- Click Open to close the Add Existing Item dialog box and to add the images to the folder. The images are added to the Images folder.
- Open the MainPage.xaml code page (either MainPage.xaml.cs or MainPage.xaml.vb).
- Place the following code beneath the Initialize() method:
Visual Basic |
Copy Code
|
‘ Create the ObservableCollection
Dim bluRayCovers As New System.Collections.ObjectModel.ObservableCollection(Of String)()
bluRayCovers.Add("Images/cover1.jpg")
bluRayCovers.Add("Images/cover2.jpg")
bluRayCovers.Add("Images/cover3.jpg")
bluRayCovers.Add("Images/cover4.jpg")
bluRayCovers.Add("Images/cover5.jpg")
‘ Pass the collection to the control as a data context
Me.DataContext = bluRayCovers
|
C# |
Copy Code
|
// Create the ObservableCollection
System.Collections.ObjectModel.ObservableCollection<string>bluRayCovers = new System.Collections.ObjectModel.ObservableCollection<string>();
bluRayCovers.Add("Images/cover1.jpg");
bluRayCovers.Add("Images/cover2.jpg");
bluRayCovers.Add("Images/cover3.jpg");
bluRayCovers.Add("Images/cover4.jpg");
bluRayCovers.Add("Images/cover5.jpg");
//Pass the collection to the control as a data context
this.DataContext = bluRayCovers;
|
- Bind the collection to the control by completing the following steps:
- Return to Design view.
- Select the C1CoverFlow control to reveal its list of properties in the Properties panel.
- Next to the ItemsSource property, click the Advanced options button and select Custom Expression.
- Set the Custom expression field to "{Binding}". This sets up the ItemsSource to pass the DataContext directly to its template, which you will create in the next step.
- Create a DataTemplate with an Image control and then bind the Image control’s Source property to the collection by completing the following steps:
- Right-click the C1CoverFlow control and select Edit Additional Templates | Edit Generated Items | Create Empty. The Create ControlTemplate Resource dialog box opens.
- In the Name (Key) field, enter “ImageTemplate”.
- Click OK to close the Create ControlTemplate Resource dialog box and create the template.
- In the Assets panel, enter “Image” into the search bar and then double-click the Image icon to add the Image control to the template.
- Select the Image control to reveal its list of properties in the Properties panel and then complete the following:
- Click the Width property’s glyph to set the Width property to Auto.
- Click the Height property’s glyph to set the Height property to Auto.
- Click the Source property’s Advanced options button, select Custom Expression, and set the Custom expression field to "{Binding}".
- Run the project and observe that the images specified in the collection are now C1CoverFlow control items.
This Topic Illustrates the Following:
The following image demonstrates the results of this topic.