Instantiating a Class in XAML

You are able to instantiate classes in XAML, defining them as either a resource, or as the value of a control's property. Let's walk through how you can do this.

Creating a Class to Bind To

Let's start by creating the class that we will instantiate in XAML. Create a folder named ViewModels folder in your project. Add a new class to this folder, and name it ProductViewModel. Add some properties to it that we can bind to (Name, ProductNumber, etc.), and set some default values for them in the class's constructor:

public class ProductViewModel {     public string Name { get; set; }     public string ProductNumber { get; set; }     public ProductViewModel()     {         Name = "Helmet";         ProductNumber = "H01"; ...

Get Pro Business Applications with Silverlight 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.