How to customize the default context menu

In my WPF application, I want all my textboxes to cut, copy and paste constraints. One way to do this is to installContextMenu ="{x:Null}"

But in doing so, I will lose the spell check suggestions that I don't want to lose. Also in my application I have 1000 text fields so I want to do this in a more streamlined way.

Any advice would be appreciated.

+3


source to share


1 answer


If you only want the menu items related to spellchecking, you can refer to this MSDN article:
How to Guide. Using the spell checker in the context menu .

If you want to apply custom ContextMenu to multiple (but not all) text fields:

  <Window.Resources>
    <ContextMenu x:Key="MyCustomContextMenu">
      <MenuItem Header="Ignore All" Command="EditingCommands.IgnoreSpellingError" />
    </ContextMenu>
  </Window.Resources>
  <Grid>
    <TextBox Height="23" Name="textBox1" Width="120" SpellCheck.IsEnabled="True"
      ContextMenu="{StaticResource MyCustomContextMenu}" />
  </Grid>

      

If you want to apply custom ContextMenu to ALL text fields :



  <Window.Resources>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="ContextMenu">
        <Setter.Value>
          <ContextMenu>
            <MenuItem
              Header="Ignore All"
              Command="EditingCommands.IgnoreSpellingError" />
          </ContextMenu>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <Grid>
    <TextBox Height="23" Name="textBox1" Width="120" SpellCheck.IsEnabled="True"  />
  </Grid>

      


Note:

  • You can move the context menu resource to the application level instead of the window level.
  • The MSDN article mentions how to get menu items through C # code and not through XAML. I could easily port the Ignore All command to XAML (code snippets above), but for spelling suggestions you will need to do some R&D.
0


source







All Articles