-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExtendedPropertyGrid.cs
160 lines (135 loc) · 4.14 KB
/
ExtendedPropertyGrid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
namespace Menees.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
#endregion
/// <summary>
/// Adds commonly needed functionality to the Windows Forms PropertyGrid.
/// </summary>
public sealed partial class ExtendedPropertyGrid : PropertyGrid
{
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
public ExtendedPropertyGrid()
{
this.InitializeComponent();
}
/// <summary>
/// Creates a new instance for the specified container.
/// </summary>
/// <param name="container">The container for this component.</param>
public ExtendedPropertyGrid(IContainer container)
{
container.Add(this);
this.InitializeComponent();
}
#endregion
#region Public Methods
/// <summary>
/// Selects the specified property on the currently selected object.
/// </summary>
/// <param name="propertyName">The name of a property to select.</param>
public void SelectProperty(string propertyName)
{
// Select the specific property in the grid if propertyName is non-empty.
if (!string.IsNullOrEmpty(propertyName))
{
// According to the following thread SelectedGridItem will never be null, and it
// can be used to get GridItem.Parent.GridItems.
// http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=542741&SiteID=1
GridItem? selectedItem = this.SelectedGridItem;
if (selectedItem != null)
{
// Search up to the root item, so we can then search only top-level properties.
while (selectedItem != null && selectedItem.GridItemType != GridItemType.Root)
{
selectedItem = selectedItem.Parent;
}
// If the name of a non-browsable property is specified, then this will
// return null. But we can't assign null to SelectedGridItem because
// it will throw an ArgumentException.
GridItem? result = selectedItem != null ? FindPropertyGridItem(selectedItem.GridItems, propertyName) : null;
if (result != null)
{
this.SelectedGridItem = result;
}
}
}
}
/// <summary>
/// Forces the grid to refresh itself in case properties were updated.
/// </summary>
public void RefreshProperties()
{
object[] selection = this.SelectedObjects;
// Try to save the name of the currently selected property.
string? selectedPropertyName = null;
if (selection != null && selection.Length > 0)
{
GridItem gridItem = this.SelectedGridItem;
if (gridItem != null && gridItem.GridItemType == GridItemType.Property)
{
selectedPropertyName = gridItem.PropertyDescriptor?.Name;
}
}
this.SelectedObjects = null;
this.SelectedObjects = selection;
// Try to restore the previously selected property.
if (selectedPropertyName.IsNotEmpty())
{
this.SelectProperty(selectedPropertyName);
}
}
#endregion
#region Private Methods
private static GridItem? FindPropertyGridItem(GridItemCollection gridItems, string propertyName)
{
GridItem? result = null;
foreach (GridItem item in gridItems)
{
if (item.GridItemType == GridItemType.Category)
{
result = FindPropertyGridItem(item.GridItems, propertyName);
if (result != null)
{
// Make sure the category is expanded to avoid an ArgumentException
// when trying to select a child grid item.
if (item.Expandable)
{
item.Expanded = true;
}
break;
}
}
else if (item.GridItemType == GridItemType.Property && item.PropertyDescriptor?.Name == propertyName)
{
result = item;
break;
}
}
return result;
}
private void Reset_Click(object sender, EventArgs e)
{
this.ResetSelectedProperty();
}
private void GridContext_Opening(object sender, CancelEventArgs e)
{
bool enabled = false;
if (this.SelectedObject != null && this.SelectedGridItem != null
&& this.SelectedGridItem.GridItemType == GridItemType.Property)
{
enabled = this.SelectedGridItem.PropertyDescriptor?.CanResetValue(this.SelectedObject) ?? false;
}
this.reset.Enabled = enabled;
}
#endregion
}
}