Drop Down List with WPF Menu Controls - c#

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.

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?

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}">....

Disable other menu items when one of the menu item is clicked c#

I have a simple menu in a WPF Client C# application where the user should be able to select only one item at a time. If one of the items is selected, the other items should be disabled until the operation is completed.
For example, I have the following menu
<MenuItem Header="Help">
<Separator></Separator>
<MenuItem Header="User Manual.." Click="UsageDocMenuItem_Click" />
<Separator></Separator>
<MenuItem Header="Login" Click="LoginItem_Click"/>
<Separator></Separator>
<MenuItem Header="About" Click="AboutMenuItem_Click"/>
<Separator></Separator>
</MenuItem>`
If user clicks Login, User manual and About should be disabled until the operation trigerred by Login is finished.
Which C# design or code method can be used to implement this?
So means there are menu items and when User clicks on one of them for example : Login
Then till the time action is completed Login should be enabled and other menu items should be disabled.
You need to create an event of button click and within the same you need to write the code to find the other menu items and disable them.
Button_click Event(...)
{
//...disable other menu items
}
Assuming you are using 'WPF', you can use commands and your ViewModel that contains these commands to determine whether the command is enabled or not. This way it is also your ViewModel that knows the 'mode' you are in.
I found the solution. I am not sure if we can use IDs of selected menu item since I want to disable the whole menu at once. I can so something like this:
<MenuItem Header="Help" Name="help">
<Separator></Separator>
<MenuItem Header="User Manual.." Click="UsageDocMenuItem_Click" />
<Separator></Separator>
<MenuItem Header="Login" Click="LoginItem_Click"/>
<Separator></Separator>
<MenuItem Header="About" Click="AboutMenuItem_Click"/>
<Separator></Separator>
</MenuItem>`
void UsageDocMenuItem_Click()
{
help.IsEnabled = false;
//Do stuff
help.IsEnabled = true;
}
By giving a name to the menu item, I can access it anywhere in the code.
Your question is not very detailed. If all you need is the basic logic/concept, then perhaps all you need is a method like this?:
public void DisableAllButOneMenuItem(int idOfSelectedMenuItem)
{
// Code that disables all itmes,
// then code that enables a single item again.
}

Adding Custom Options to WPF RichTextBox Context Menu

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 :)

Context Menu on TreeViewItem Calls TreeViewItem's Method?

I have a TreeView setup so that each TreeViewItem has right-click context menu applied as a Style. Something like:
<Grid.Resources>
<ContextMenu x:Key="contextMenu">
<MenuItem Header="Save" IsEnabled="{Binding Path=Saveable}"/>
<MenuItem Header="Copy" IsEnabled="{Binding Path=Copyable}"/>
<MenuItem Header="Remove" IsEnabled="{Binding Path=Removeable}"/>
</ContextMenu>
<Style TargetType="TreeViewItem">
<Setter Property="ContextMenu" Value="{StaticResource contextMenu}" />
</Style>
</Grid.Resources>
Saveable, Copyable and Removeable are properties that come from the object that's used as the TreeViewItem.
What I'm looking for is when the user clicks on a MenuItem, it would click on the appropriate method of the selected object. So clicking on the "Save" MenuItem would call object.Save(), "Copy" calls object.Copy(), etc. But I'm not sure what the syntax would look like, or whether the idea is actually acceptable in terms of typical WPF style. I know I can just create a new event handler in the encompassing window, but I'd prefer the selected item itself to handle the event.
Thoughts?
Thanks!
Unfortunately, I don't think that there is an automated way of doing this. The closest option would be to setup a RoutedUICommand for each item in the ContextMenu, and then create a CommandBinding for each in your class. If you want those to go to the TreeViewItem, you'll probably need to subclass TreeViewItem and set up the CommandBindings there.
The one option that I thought might work would be to add an EventSetter for MenuItem.Click to the TreeViewItem style. However, that did not work - probably because the items in the ContextMenu are in a different visual tree from the TreeViewItems.

Categories