简体   繁体   中英

Can't set a custom contextmenu to a table in richtextbox

<Window.Resources>
  <ContextMenu x:Key="TableContextMenu">
   <MenuItem Command="Copy" />
   <MenuItem Header="asdasdsad" />
  </ContextMenu>
  <Style TargetType="{x:Type Table}">
   <Setter Property="ContextMenu" Value="{StaticResource TableContextMenu}" />
  </Style>
 </Window.Resources>

Does anybody have an idea?

Got it, you have to manually open the contextmenu ..

<Style TargetType="{x:Type TableCell}">
        <EventSetter Event="ContextMenuOpening" Handler="Table_ContextMenuOpening" />
        <Setter Property="ContextMenu" Value="{StaticResource TableContextMenu}" />
    </Style>

In the contextmenu opening handler you have to set the handled param to true and open the contextmenu

lastTableCell.ContextMenu.IsOpen = true;

If your commands in the contextmenu are grayed out: afaik this is a bug and you have to put your command bindings directly into the contextmenu in XAML

<ContextMenu x:Key="TableContextMenu">
        <ContextMenu.CommandBindings>
            <CommandBinding Command="{x:Static main:MainWindow.AddRowAboveCommand}"
                    CanExecute="CanExecuteAlways"
                    Executed="AddRowAbove_Executed" />
        </ContextMenu.CommandBindings>

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM