|
|
Questions and answers for Controls Context Menu.
|
67. Controls Context Menu
|
| 67.1 How do I make
the Context Menu appear only when clicked at certain portions of the Control ?
|
| 67.2 How can I
enable a Context Menu on a disabled Control ?
|
| 67.3 How can I
create a shared Context Menu ?
|
| 67.4 How do I make
the Context Menu to close after a set time interval?
|
67.1 How do I make the Context Menu appear only when clicked at certain
portions of the Control ?
|
|
You can listen to the Popup event, determine where the mouse was clicked and
selectively make the menu items visible in the menu as follows.
|
[C#]
private void contextMenu1_Popup(object sender, EventArgs e)
{
// Get current mouse click position in the control
// (assuming pictureBox1 is the control):
Point ptClick = pictureBox1.PointToClient(Control.MousePosition);
// Get the rectangle where you want to show the context menu.
Rectangle preferredClickRect = new Rectangle(0, 0, 50, 50);
if (preferredClickRect.Contains(ptClick))
{
// Show all the menu items so that the menu will appear
foreach (MenuItem item in contextMenu1.MenuItems)
item.Visible = true;
}
else
{
// Hide all the menu items so that the menu will not appear
foreach (MenuItem item in contextMenu1.MenuItems)
item.Visible = false;
}
}
|
67.2 How can I enable a Context Menu on a disabled Control ?
|
|
This example uses the ContextMenuService to display a Context Menu for a button
that is disabled.
|
[XAML]
<Button Height="30" Content="Disabled Button"
IsEnabled="False"
ContextMenuService.ShowOnDisabled="True">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
<MenuItem Header="Item 3"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
|
67.3 How can I create a shared Context Menu ?
|
|
One common way of moving the focus around in a user interface on Windows is to
use the Tab key to move sequentially between the controls. The contents of a
WPF toolbar seems to act like a kind of tabbing black-hole. The focus goes in
there but never comes out again. Try pasting this XAML code into the XAMLPad to
see the problem.
|
[XAML]
<DockPanel>
<ToolBar DockPanel.Dock="Top">
<Button Content="B"
Command="EditingCommands.ToggleBold" />
<Button Content="U"
Command="EditingCommands.ToggleUnderline" />
<Button Content="I"
Command="EditingCommands.ToggleItalic" />
</ToolBar>
<RichTextBox />
</DockPanel>
|
|
Fortunately this can be easily fixed by using the "KeyboardNavigation" attached
property and changing the TabNavigation to 'Continue'. This allows the tab
focus to move out of the toolbar and back to the other elements of the UI.
|
[XAML]
<DockPanel>
<ToolBar DockPanel.Dock="Top"
KeyboardNavigation.TabNavigation="Continue">
<Button Content="B"
Command="EditingCommands.ToggleBold" />
<Button Content="U"
Command="EditingCommands.ToggleUnderline" />
<Button Content="I"
Command="EditingCommands.ToggleItalic" />
</ToolBar>
<RichTextBox />
</DockPanel>
|
67.4 How do I make the Context Menu to close after a set time interval?
|
|
To automatically close the context menu after a set time interval, you can use a
Timer and send an Esc keystroke after the desired time interval as shown.
|
[C#]
private void timer1_Tick(object sender, System.EventArgs e)
{
SendKeys.Send("{ESC}");
timer1.Stop();
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
//set interval to 5 seconds
timer1.Interval = 5000;
timer1.Start();
}
|
|