Adding Custom Options to WPF RichTextBox Context Menu - c#

I have a WPF RichTextBox and I want to add some more options to the default context menu. I dont want to loose the default menu options (Cut, Copy, Paste). Can you help me out?
Thanks

Extending the previous answer:
<RichTextBox x:Name="rtbTest">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Cut"/>
<MenuItem Command="ApplicationCommands.Copy"/>
<MenuItem Command="ApplicationCommands.Paste"/>
<MenuItem Header="Custom Item"/>
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
Each command is provided with a default UI Text and Key Gesture, by omitting them (in this case the 'Header') from your definition they will fallback to the default, which will be in the users own preferred language.

I am afraid that this might be possible or not, but an easy workaround for this(that you might too be aware of) is adding all these application commands back as the context menu item and then adding you custom menu items after that:
<RichTextBox x:Name="rtbTest">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Cut" Command="ApplicationCommands.Cut"/>
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
<MenuItem Header="Custom Item"/>
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
This is a workaround but you can easily achieve your purpose using this :)

Related

Menu item in Navigation region (WPF Prism)

I have a modular WPF Prism application as described in this question: Sub-modules within a Region in WPF Prism
Every main module (A, B and C) registers a NavigationItemView in the NavigationRegion and an ItemView in the MainRegion. So whenever a NavitaionItemView is pressed, the corresponding ItemView is loaded in the MainRegion.
But now, I want to add a button in the NavigationRegion that is not a NavigationItemView so it has no associate ItemView loaded in the MainRegion; but instead, it is a Menu (with an image and some menu items). Something like this:
<Menu>
<MenuItem ToolTip="Settings" Background="#D6D6DC" BorderBrush="#D6D6DC">
<MenuItem.Header>
<StackPanel>
<Image Source="Images/settings2.png" Width="30" Height="30" />
</StackPanel>
</MenuItem.Header>
<MenuItem Header="_Language">
<MenuItem x:Name="MenuItem_English" IsCheckable="True" Header="English" Click="MenuItem_Click" />
<MenuItem x:Name="MenuItem_Spanish" IsCheckable="True" Header="Spanish" Click="MenuItem_Click"/>
</MenuItem>
<Separator/>
<MenuItem Header="Load file" x:Name="MenuItem_LoadFile" Click="LoadFile" />
</MenuItem>
</Menu>
What is the best approach to do this within Prism framework? I am quite new to Prism, so the further details on how to organize the classes the better. Thanks in advance!
Clarification:
My Shell has two regions (NavigationRegion -which is a ToolBar- and MainContentRegion - which is a ContentControl-). Every module registers a ModuleNavigationView in the NavigationRegion and a ModuleView in the MainContentRegion. Whenever the ModuleNavigationView is pressed, the ModuleView is shown in the MainContentRegion.
I want to add another item (Menu) to the toolbar (just like the ModuleNavigationView's), but this one is a special one because it doesn't show anything in the MainContentRegion, but instead shows a menu with some options.
And my question is if this settings menu should be another Module. What's the best approach to accomplish this?

How to add a check mark on context menu item?

I have a WPF application with Caliburn Micro. On my DataGrid, I have a context menu with two items, which represent two options two select from. How can I add a check mark on the items?
Here is my code:
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="Treat Invalid Billing Address As Error" cal:Message.Attach="[Event Click] = [Action TreatInvalidBillingAddressAs('Error')]" />
<MenuItem Header="Treat Invalid Billing Address As Warning" cal:Message.Attach="[Event Click] = [Action TreatInvalidBillingAddressAs('Warning')]" />
</ContextMenu>
</DataGrid.ContextMenu>
...
public void TreatInvalidBillingAddressAs(string errorOrWarning)
{
SelectedFirstEntry.BillingAddressValidation = errorOrWarning;
Revalidate();
}
Thanks
As mentioned in the comments to control checked status of the MenuItem you could bind MenuItem.IsChecked property to the BillingAddressValidation property of your view model.
Since this is a property of view model against each row and ContextMenu is defiend against whole DataGrid you can use SelectedFirstEntry item.
<MenuItem ... IsChecked="{Binding SelectedFirstEntry.BillingAddressValidation, Converter={StaticResource ErrorToBoolConverter}}"/>

Changing ContextMenu Appearance

currently I have the basic radgridview Context menu, I'm trying to get it to look like the context menu in here from this demo ContextMenu
May I get some tips or suggestions, I'm not quite sure where to start.
My Contextmenu code:
<telerik:RadGridView.ContextMenu>
<ContextMenu>
<MenuItem Header="View Contact" cal:Message.Attach="[Event Click] = [Action Open()]"/>
</ContextMenu>
</telerik:RadGridView.ContextMenu>
Since you are asking for a direction here.
I would suggest you to read about Styles and templates in WPF (MSDN)
In WPF the controls are view-less. The view is controlled by the style/template that you apply to the control.
For instance, in your case if you can get the style of the Context menu control you have mentioned (say it's named "FancyContextMenu" in resource dictionary).
Then you just need to modify your code to below:
<telerik:RadGridView.ContextMenu>
<ContextMenu Style="{StaticResource ResourceKey=FancyContextMenu}">....

Assign ContextMenu Item Click event in a resourceDictionary.xaml file

I want to know if I could assign item click event to this context menu that it is placed in an .Xaml file of type ResourceDictionary which one does not have code behind class, however i know that i can manually assign a .cs class to this .xaml.
But I dont want to assign a file class to it because I already have another .Xaml file with its own .cs file class in which i would want to register the itemClick event.
My code in the resourcedictionary file is:
<ContextMenu>
<MenuItem Header="Show SoundRecorder" Click="ItemClick"></MenuItem>
<Separator/>
<MenuItem Header="Start Recording Now"></MenuItem>
<MenuItem Header="Stop Recording"></MenuItem>
<Separator/>
<MenuItem Header="About"></MenuItem>
<MenuItem Header="Exit SoundRecorder"></MenuItem>
</ContextMenu>
How could i achieve this approach?
Create a command in a data context for the view with the context menu.
public ICommand ItemClickCommand{ get; set; }
Then, you can bind the MenuItem's Command property to the new command.
<ContextMenu>
<MenuItem Header="Show SoundRecorder" Command="{Binding ItemClickCommand}"></MenuItem>
<Separator/>
<MenuItem Header="Start Recording Now"></MenuItem>
<MenuItem Header="Stop Recording"></MenuItem>
<Separator/>
<MenuItem Header="About"></MenuItem>
<MenuItem Header="Exit SoundRecorder"></MenuItem>
</ContextMenu>
At runtime, if WPF finds the command in the data context it will bind the menu item to it. Otherwise, it will issue a warning in the Output console.
I will not go here on implementing the ICommand interface. There are plenty of googlable implementations.

Drop Down List with WPF Menu Controls

I am looking for a way to add a drop down list in WPF to a menu. This used to be really easy in winforms and so I am expecting you experts to know just now to do it in WPF. Thanks.
Sorry if this is a bad question, it is late and I don't want to think.
It is very easy to add any UIElement to any control, You can just add Combobox to a Menu control and create menu as bellow.
<Menu>
<MenuItem Header="File">
<MenuItem Header="Open"/>
<MenuItem Header="Close"/>
<Separator/>
<ComboBox Width="85" Height="21.96" />
</MenuItem>
</Menu>
While this is very easy to do as Jobi Joy has shown, I think it has horrible usability. The Menu control supports multiple levels of menu items and I would go down that route for UI consistency.

Categories