This is related to the post @ http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/bda84c83-d82a-4992-b2c1-13a4bc4660c7.
I have been creating a databound ListView and I have been enhancing it to support more of the design-time features available in Visual Studio.
The latest addition: A method that automatically adds column headers to the ListView control based on the properties exposed by the databound objects. The code is really simple, and is shown below:
public void GenerateColumnsForDataSource()
{
CurrencyManager cm = (CurrencyManager)_lv.Parent.BindingContext[_lv.DataSource, _lv.DataMember];
PropertyDescriptorCollection props = cm.GetItemProperties();
foreach (PropertyDescriptor pd in props)
{
DataBoundListView.DataBoundColumnHeader header = _hostSvc.CreateComponent(typeof(DataBoundListView.DataBoundColumnHeader),
String.Format("dbch_{0}", pd.Name)) as DataBoundListView.DataBoundColumnHeader;
GetPD(header, "DataField").SetValue(header, pd.Name);
GetPD(header, "Text").SetValue(header, pd.DisplayName);
_lv.Columns.Add(header);
}
GetPD(this.Component, "View").SetValue(this.Component, View.Details);
_lv.Bind();
}
This code uses the designer host to create each column header (because column headers are components), and then uses property descriptors to set the different column header properties. The last step is to force the binding process in order to refresh the view.
This code properly generates undo entries. But, it generates one undo entry for each single action. To clarify the picture here, note that each generated column generates 3 undo entries: One for the addition of the column, and then one for the
DataField property and one for the
Text property.
The question is: How can I force the Visual Studio designer to merge all these actions into a single, undoable action so the developer doesn't have to undo each of the steps?
Thank you all!
MCP