User Interface for .NET / User's Guide / Command Bars / Using Command Contexts

Using Command Contexts
 Creating And Customizing Command Contexts

The following code demonstrates how to create and customize command contexts:

C#
Copy Code
//create a command context
NCommandContext context = new NCommandContext();

//customize context's visual properties
context.Properties.Text = "MyCommandContext";
context.Properties.Style = CommandStyle.ImageAndText;

//specify imagelist and image index
context.Properties.ImageList = myImageList;
context.Properties.ImageIndex = 0;
Visual Basic
Copy Code
'create a command context
Dim context As NCommandContext = New NCommandContext()

'customize context's visual properties
context.Properties.Text = "MyCommandContext"
context.Properties.Style = CommandStyle.ImageAndText

'specify imagelist and image index
context.Properties.ImageList = myImageList
context.Properties.ImageIndex = 0
Both NCommand and NCommandContext objects expose a NCommandProperties property which contains common information about a command.
 Creating Commands From Contexts

You can create a command from a context using the following code:

C#
Copy Code
//assume that we have a valid context created
NCommand command = NCommand.FromContext(myCommandContext);
Visual Basic
Copy Code
'assume that we have a valid context created
Dim command As NCommand = NCommand.FromContext(myCommandContext)

The FromContext method will initialize a new command (command type depends on the context's type), update its properties and associate it with the context.

 Organizing Contexts in Ranges

You can group contexts in ranges using the RangeID property, which should point to a valid ID of a NRange  object. This feature is primarily used by the NCommandBarsManager component.

 

The following example demonstrates how to create a range and two command contexts which RangeID property points to that range:

C#
Copy Code
//create the range
NRange range = new NRange();
range.ID = 1;
range.Name = "Standard";

//create two command contexts
NCommandContext context;

context = new NCommandContext();
context.RangeID = 1;

context = new NCommandContext();
context.RangeID = 1;
Visual Basic
Copy Code
'create the range
Dim range As NRange = New NRange()
range.ID = 1
range.Name = "Standard"

'create two command contexts
Dim context As NCommandContext

context = New NCommandContext()
context.RangeID = 1

context = New NCommandContext()
context.RangeID = 1

The NCommandBarsManager component exposes Contexts and Ranges properties which help manager to organize contexts by ranges.

For more information about building docking command bars framework see Docking Toolbars Framework tutorial. 

 Functionality

A NCommandContext object exposes the following functionality:

 

Events:

Event Description
 Executing Fired just before the context is executed. Cancelable.
 Executed Fired when the context is executed.


Public methods:

 

Method Description
 Execute Performs context specific action.
 CanExecute Checks whether the context can be executed.
 GetEnabled Gets a boolean value whether commands associated with the context are enabled.
 SetEnabled Sets the Enabled property of all associated commands to the specified value.
 GetChecked Gets a boolean value whether commands associated with the context are checked.
 SetChecked Sets the Checked property of all associated commands to the specified value.
 UpdateCommands Forces re-measure and update of all commands sharing this context.
 GetCommands Gets all the commands sharing this context.
 GetNestedContexts Gets all the contexts and their children that are contained by the context. 
See Also