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.
Related
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?
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 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}}"/>
I have the following context menu on the header of a column in a datagridview.
<DataGridCheckBoxColumn Binding="{Binding Include,UpdateSourceTrigger=PropertyChanged}" Width="50">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Export">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Alle auswaehlen"/>
<MenuItem Header="Alle abwahelen"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
As you can see, the context menu is static. How can I map the Command attribute to static methods in my code? All examples I found online were for flexible binding or for cut/copy.
You could use the click event instead:
<MenuItem Header="Alle auswaehlen" Click="MenuItem_Click_1"/>
And then have this method in your code:
private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
}
I don't see why that ContextMenu would be static; it appears to be created for each text block, which will be created for each header. If you only have one header, then I suppose it's de facto static.
Either way, if you want to bind to static command MyCommand of class MyNamespace.MyClass, then you use the following syntax:
<MenuItem Header="header" Command="{x:Static mynamespace:MyClass.MyCommand}"/>
Note that you need to have specified the xml namespace on the parent XAML object as follows:
xmlns:mynamespace="clr-namespace:MyNamespace"
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 :)