Hi Luis,
To make the commands pane of a PropertyGrid visible for objects that
expose
verbs, we should set the ProprtyGrid's CommandsVisibleIfAvailable property
to true. In fact, this property is set to true by default.
Except that, there's nothing left to get a PropertyGrid to display
commands
for objects that expose verbs.
The following is an example that implements a simple form designer using
DesignSurface. The sample contains a main form and a HostSurface class
derived form DesignSurface class. The main form displays a Form designer
and a PropertyGrid.
In the HostSurface there¡¯s an event handler for the ISelectionService's
SelectionChanged event so as to refresh the PropertyGrid when the selected
objects on the form designer changed.
The sampe code is as follows:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Collections;
// the definition of HostSurface
public class HostSurface : DesignSurface
{
private ISelectionService _selectionService;
public HostSurface() : base()
{
// Set SelectionService - SelectionChanged event handler
_selectionService =
(ISelectionService)(this.ServiceContainer.GetService(typeof(ISelectionServic
e)));
_selectionService.SelectionChanged += new
EventHandler(selectionService_SelectionChanged);
}
// When the selection changes this sets the PropertyGrid's
selected component
private void selectionService_SelectionChanged(object sender,
EventArgs e)
{
if (_selectionService != null)
{
ICollection selectedComponents =
_selectionService.GetSelectedComponents();
PropertyGrid propertyGrid =
(PropertyGrid)this.GetService(typeof(PropertyGrid));
if (propertyGrid != null)
{
object[] comps = new
object[selectedComponents.Count];
int i = 0;
foreach (Object o in selectedComponents)
{
comps
= o;
i++;
}
propertyGrid.SelectedObjects = comps;
}
}
}
public void AddService(Type type, object serviceInstance)
{
this.ServiceContainer.AddService(type, serviceInstance);
}
}
// the event handler for the main form's load event
private void Form1_Load(object sender, EventArgs e)
{
// add a custom form designer on the main form
HostSurface surface = new HostSurface();
surface.BeginLoad(typeof(Form));
Control view = (Control)surface.View;
view.Dock = DockStyle.Fill;
view.Parent = this;
surface.AddService(typeof(PropertyGrid),
this.propertyGrid1);
// add a TextBox on the form designer
IDesignerHost idh =
(IDesignerHost)surface.GetService(typeof(IDesignerHost));
IToolboxUser tbu = idh.GetDesigner(idh.RootComponent as
IComponent) as IToolboxUser;
if (tbu != null)
{
ToolboxItem toolboxitem = new
ToolboxItem(typeof(TabControl));
tbu.ToolPicked((System.Drawing.Design.ToolboxItem)toolboxitem);
}
}
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.