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?
Related
I have been working with c# for some time now but surprisingly I have never dealt with context menus before. I have a listView control in my universal windows 8.1 app. Now I am trying to get a context menu to popup for each item in the listView (they are all the same type of object and are added to the list as the user adds entries). I have run into several problems with this and have looked at code examples and they seem to be leading in different directions. Firstly when I right click on an item in the list it does not fire the ListView_RightTapped event.
<ListView x:Name="lstvwHours" HorizontalAlignment="Left" Height="264" Margin="427,77,0,0" VerticalAlignment="Top" Width="357" RightTapped="lstvwHours_RightTapped">
Secondly in Microsoft's context menu code example they say to use the PopupMenu class but in other code I've seen it coded into the XAML.
And lastly After the one context menu button is clicked I want it to fire a delete method.
private async void lstvwHours_RightTapped(object sender,
RightTappedRoutedEventArgs e)
{
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Delete"/*do I put the method to call here?*/));
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
}
Here's an example.
In this case you can wire-up the commands that get invoked from your menuitem onto your view-model.
<ListView>
<ListViewItem Content="One">
<ListViewItem.ContextMenu>
<ContextMenu>
<MenuItem Header="Insert"
Command="{Binding DataContext.InsertQuery, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
<MenuItem Header="Delete"
Command="{Binding DataContext.DeleteQuery, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
</ContextMenu>
</ListViewItem.ContextMenu>
</ListViewItem>
</ListView>
I have a really strange behaviour, and I hope someone can help me out.
I have the following XAML layout:
<StackPanel Orientation="Horizontal">
<Menu>
<Menu.Items>
<MenuItem Padding="2,0,2,0">
<MenuItem.Header>
<Button Content="Details"
Click="Details_Click" />
</MenuItem.Header>
</MenuItem>
</Menu.Items>
</Menu>
<Button Content="Details"
Click="Details_Click" />
</StackPanel>
Please notice that both buttons have the same Event registered.
The Details_Click Event looks like this:
private void Details_Click(object sender, RoutedEventArgs e)
{
var viewer = new DictionaryViewer();
viewer.ShowActivated = true;
viewer.Show();
viewer.Topmost = true;
viewer.Topmost = false;
viewer.Activate();
viewer.Focus();
e.Handled = true;
return;
}
Now I am facing the problem, even with all the code from above the Window doesnt show up activated when I press the button inside the Menu but outside of it works with just .Activate();.
(How I know that window isnt activated: need 2 clicks to close/minimize/maximize it)
Why would my XAML Layout ruin the Activation of the DictionaryViewer(); window, with the button inside Menu?
(To your information the DictionaryViewer is totally empty, its a fresh window nothing implemented yet)
Edit:
Yes, I know there is the MenuItem_Click Event that may make it work, but I need/want the button inside the Menu how can I fix this issue?
THe reason this is happening is because the Button inside the MenuItem is gaining Focus after the Window has opened.
If you set the Focusable property of the button inside MenuItem, this fixes the issue.
E.g.
<StackPanel Orientation="Horizontal">
<Menu>
<Menu.Items>
<MenuItem Padding="2,0,2,0">
<MenuItem.Header>
<Button Content="Details"
Click="Details_Click"
Focusable="False" />
</MenuItem.Header>
</MenuItem>
</Menu.Items>
</Menu>
<Button Content="Details"
Click="Details_Click" />
</StackPanel>
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.
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 :)
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.