ComponentOne Extended Library for WPF and Silverlight
Binding C1PropertyGrid to a Class
PropertyGrid > Task-Based Help > Binding C1PropertyGrid to a Class

 

PropertyGrid for WPF allows you to easily bind the control to a class. At run time items in the class can be browsed and edited using the C1PropertyGrid control. For example, add a simple Customer class to your project defined as follows:

Visual Basic
Copy Code
Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
Private _EMail As String
    Public Property EMail() As String
        Get
            Return _EMail
        End Get
        Set(ByVal value As String)
            _EMail = value
        End Set
    End Property
Private _Address As String
    Public Property Address() As String
        Get
            Return _Address
        End Get
        Set(ByVal value As String)
            _Address = value
        End Set
    End Property
Private _CustomerSince As DateTime
    Public Property CustomerSince() As DateTime
        Get
            Return _CustomerSince
        End Get
        Set(ByVal value As DateTime)
            _CustomerSince = value
        End Set
    End Property
Private _SendNewsletter As Boolean
    Public Property SendNewsletter() As Boolean
        Get
            Return _SendNewsletter
        End Get
        Set(ByVal value As Boolean)
            _SendNewsletter = value
        End Set
    End Property
Private _PointBalance As System.Nullable(Of Integer)
    Public Property PointBalance() As System.Nullable(Of Integer)
        Get
            Return _PointBalance
        End Get
        Set(ByVal value As System.Nullable(Of Integer))
            _PointBalance = value
        End Set
    End Property
End Class

C#
Copy Code
public class Customer
{
  public string Name { get; set; }
  public string EMail { get; set; }
  public string Address { get; set; }
  public DateTime CustomerSince { get; set; }
  public bool SendNewsletter { get; set; }
  public int? PointBalance { get; set; }
}

 

You could build a user interface to display and edit customers using the following code:

Visual Basic
Copy Code
Public Sub New()
    InitializeComponent()
   
    ' Create object to browse
    Dim customer = New Customer()
   
    ' Create C1PropertyGrid
    Dim pg = New C1PropertyGrid()
    LayoutRoot.Children.Add(pg)
   
    ' Show customer properties
    pg.SelectedObject = customer
End Sub

C#
Copy Code
public Page()
{
  InitializeComponent();

  // Create object to browse
  var customer = new Customer();

  // Create C1PropertyGrid
  var pg = new C1PropertyGrid();
  LayoutRoot.Children.Add(pg);

  // Show customer properties
  pg.SelectedObject = customer;
}

 

Run the application and observe that the resulting application would look similar to the following:

 

 

This simple UI allows users to edit all the properties in the Customer objects. It was built automatically based on the object's properties and will be automatically updated if you add or modify the properties in the Customer class.

Notice that properties are shown in alphabetical order by default. You can change this by setting the C1PropertyGrid.PropertySort property; for more information see Sorting Members in C1PropertyGrid.