Like the standard Microsoft PropertyGrid control, the WPF PropertyGrid control works based on a SelectedObject property. Once this property is set, the control displays the object's public properties and allows the user to edit them. In this quick start, we add PropertyGrid to the WPF application to create a UI to display and edit information in the PropertyGrid control.
To bind an object with PropertyGrid, follow these steps:
XAML |
Copy Code
|
---|---|
<c1:C1PropertyGrid Margin="184,0,0,0" x:Name="propertyGrid"></c1:C1PropertyGrid> |
To customize and connect the C1PropertyGrid control to the Button control, complete the following steps:
XAML |
Copy Code
|
---|---|
<c1:C1PropertyGrid Margin="184,0,0,0" x:Name="propertyGrid" PropertySort="CategorizedAlphabetical" AutoGenerateProperties="False" SelectedObject="{Binding ElementName=TextButton, Mode=OneWay}" EditorWidth="60"> <c1:C1PropertyGrid.PropertyAttributes> <c1:PropertyAttribute Category="Appearance" DisplayName="Background Color" MemberName="Background" /> <c1:PropertyAttribute Category="Appearance" DisplayName="Border Color" MemberName="BorderBrush"/> <c1:PropertyAttribute Category="Appearance" DisplayName="Visibility" MemberName="Visibility" /> <c1:PropertyAttribute Category="Size" DisplayName="Button Height" MemberName="Height" /> <c1:PropertyAttribute Category="Size" DisplayName="Button Width" MemberName="Width" /> <c1:PropertyAttribute Category="Text" DisplayName="Button Text" MemberName="Content" /> <c1:PropertyAttribute Category="Text" DisplayName="Text Color" MemberName="Foreground" /> <c1:PropertyAttribute Category="Text" DisplayName="Text Size" MemberName="FontSize" /> </c1:C1PropertyGrid.PropertyAttributes> </c1:C1PropertyGrid> |
You will notice in Design view that the C1PropertyGrid control now reflects all of the button’s properties.
PropertyAttribute |
Category |
DisplayName |
MemberName |
[0] PropertyAttribute |
Appearance |
Background Color |
Background |
[1] PropertyAttribute |
Appearance |
Border Color |
BorderBrush |
[2] PropertyAttribute |
Appearance |
Visibility |
Visibility |
[3] PropertyAttribute |
Size |
Button Height |
Height |
[4] PropertyAttribute |
Size |
Button Width |
Width |
[5] PropertyAttribute |
Text |
Button Text |
Content |
[6] PropertyAttribute |
Text |
Text Color |
Foreground |
[7] PropertyAttribute |
Text |
Text Size |
FontSize |
Here, the Category identifies what section the item appears in, the DisplayName indicates the name displayed for the item and the MemberName indicates the actual name of the member.
To run your application and observe the run-time behavior of the PropertyGrid control, complete the following steps:
To bind a class with PropertyGrid, follow these steps:
XAML |
Copy Code
|
---|---|
<c1:C1PropertyGrid Margin="130,12,30,12" x:Name="propertyGrid"></c1:C1PropertyGrid> |
To customize and connect the PropertyGrid control to the class, complete the following steps:
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 int? PointBalance { get; set; } public bool SendNewsletter { get; set; } } |
C# |
Copy Code
|
---|---|
// Create object to browse var customer = new Customer(); // Show customer properties propertyGrid.SelectedObject = customer; |
Observe the output appears similar to the following: