PropertyGrid
The PropertyGrid
control in Windows Forms uses reflection to display all of the properties on any object. You can change the properties.
Property changes are reflected immediately in the user interface. The PropertyGrid
looks the same as the property dialogs in Visual Studio.
In this example, we create a Dictionary
and then assign it as the SelectedObject
. We can then visually browse the Count
, all the Keys
, and all the Values of the Dictionary
instance.
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Create an example collection. var dictionary = new Dictionary<string, string>(); dictionary.Add("cat", "blue"); dictionary.Add("dog", "green"); // Assign SelectedObject. propertyGrid1.SelectedObject = dictionary; } } }
To get started, open your project and add a PropertyGrid
. You can change the object whose properties are displayed in the PropertyGrid
by assigning to the SelectedObject
property.
Button
and assigned it to the SelectedObject
property.Button
in the grid and also the Text.SelectedObject
With PropertyGrid
, we need to set the SelectedObject
property. You can do this in the Visual Studio designer and in an event handler such as Form1_Load
.
The PropertyGrid
control in Windows Forms provides a quick way to mutate the object model in your application. It is not as easy to use as a custom solution.