Adding ContextMenu to TextBlock : ContextMenu « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="ContextMenu">
        <StackPanel>
          <Border Margin="8" Background="#EEE" Width="150" Height="50"  CornerRadius="2">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Header="Sub One" InputGestureText="Ctrl+L" />
                <MenuItem Header="Sub Two (With an Icon)" InputGestureText="Ctrl+A">
                  <MenuItem.Icon>
                    <Ellipse Width="16" Height="16" Fill="LightBlue" />
                  </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Sub Three" />
                <Separator />
                <MenuItem Header="Sub Four">
                  <MenuItem Header="Sub One" />
                  <MenuItem Header="Sub Two" />
                  <MenuItem Header="Sub Three" />
                </MenuItem>
                <MenuItem Header="Sub Five" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock Foreground="#AAA" VerticalAlignment="Center" HorizontalAlignment="Center">(Right-Click Me)</TextBlock>
          </Border>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>
WPF Context Menu Demo








24.36.ContextMenu
24.36.1.Adding ContextMenu to TextBlockAdding ContextMenu to TextBlock
24.36.2.Grid with ContextMenuGrid with ContextMenu
24.36.3.Display a Context Menu with OpacityDisplay a Context Menu with Opacity