Remove checkmark from MenuItems? - c#

I have a context menu that appears when a user right clicks which contains two menu items. The first item has a checkmark instead of the icon, and then a checkmark is placed on whichever one is clicked the next time the user right clicks. I have both IsCheckable and IsChecked set to 'False', but the checkmark still appears. Not sure what I'm doing wrong, any ideas?
This is the first time I right click, I don't want that check mark there.
This is what shows if I selected "add waypoint" the first time, and right clicked again. It should show this every time, but if I ever click "add known object", the checkmark always appears.
<ContextMenu Name="nodeContextMenu" >
<MenuItem x:Name="ko" IsCheckable="False" IsChecked="False" Header="Add Known Object" Click="Ko_Click" >
<MenuItem.Icon>
<Image Source="ko.png" Height="7.5" Width="7.5" />
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="wa" IsCheckable="False" IsChecked="False" Header="Add Waypoint" Click="Wa_Click" >
<MenuItem.Icon>
<Image Source="w.png" Height="7.5" Width="7.5" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>

I found various elaborate WPF solutions for getting rid of the menu item checkmark to be really painful, so I ditched them all. Instead I solved it in a very simple way by having two menu items for what is effectively one menu item at runtime, and use Visibility=Collapsed to alternate between the two at runtime. This is incredibly simple whether you use bindings or events.

I used to have these menuitems as radio buttons, and had a method related to the radio buttons that did a function using the .IsChecked method. I had forgotten about that and it kept the first item checked every time. So I just got rid of it and it works fine. The snippet of code causing it is below:
knownObjectMenuItem.IsChecked = //random stuff

Related

Editing a local resource's visibility in codebehind

I am fairly new to WPF and have struggled trying to find an answer to this so maybe I am going about it the wrong way.
I have a ContextMenu that I wish to use multiple places so I have it defined as a Resource:
<Window.Resources>
<ContextMenu x:Key="MainContextMenu">
<MenuItem Header="Select All" Click="SelectAllButton_Click"/>
<MenuItem Header="Clear All" Click="ClearAllButton_Click"/>
<MenuItem Header="Export" Click="ExportButton_Click" Name="ExportCM"/>
<MenuItem Header="Priority" Click="PriorityButton_Click" Name="PriorityCM"/>
<MenuItem Header="Cancel" Click="CancelButton_Click" Name="CancelCM"/>
<MenuItem Header="Reallocate" Click="ReallocateButton_Click" Name="ReallocateCM"/>
<MenuItem Header="Release" Click="ReleaseButton_Click" Name="ReleaseCM"/>
<MenuItem Header="Hazard" Click="HazardButton_Click" Name="HazardCM"/>
<MenuItem Header="Reset" Click="ResetButton_Click" Name="ResetCM"/>
</ContextMenu>
</Window.Resources>
I also have a Menu with items with identical headers and click handlers but the Menu is not a resource since I only needed to place it once. With the Menu I am able to edit the visibility of certain MenuItems in codebehind. I would like to be able to do the same to my ContextMenu.
How can I access an individual ContextMenu MenuItem and set its visibility in code behind if my ContextMenu is a locally defined static resource?
You're looking for Custom Controls in WPF. You can extend a WPF control where you can add all your Menus / Menus Items and handle them as objects using XAML.
http://wpftutorial.net/HowToCreateACustomControl.html
After doing that, inside the Window you want to use your custom Menu, you need to add the namespace like in:
xmlns:controls="clr-namespace:YourProject.YourControl"
And then you can use your menu like this:
<controls:YourControl>
Also take a look at DataTemplates.
ContextMenu ctxmenu = (ContextMenu)this.Resources["MainContextMenu"];
And if you assign this context menu to say Button:Btn1, then Btn1.ContextMenu will give you the context menu of button Btn1.
To access a particular menuitem, you have btn1.ContextMenu.Items collections to play with.

Get a MenuItem to switch to a different visible window when clicked

I am working with C# in MS Visual Studio Community 2013. I am trying to set up a MenuItem so that when clicked it causes the current window to hide itself and another window appears at the same location.
I have a couple of books on the subject and I've also spent lots of time searching for answers online. However, I simply can't find out how to set this up when it's something that should be relatively straightforward to do.
I've done it before with older versions of C#, but it just seems impossible to sort out with Visual Studio Community 2013.
At this stage I have a menu and menu items set up something like the 'much simplified' following:
<Menu HorizontalAlignment="Left" Height="21" Margin="10,10,0,0" VerticalAlignment="Top" Width="1264" Grid.ColumnSpan="2" FontWeight="Medium" FontSize="14">
<MenuItem Header="Item Type" Margin="0" Width="100" Height="21" FontSize="11" Click="MenuItem_Click">
<MenuItem Header="Item Action" HorizontalAlignment="Left" Width="185"/>
</MenuItem>
</Menu>
Most, if not all of that was generated automatically, as I set up the menu in Visual Studio.
Also, if I right click on the 'Item Action' menu item, a popup appears with a 'view code' option. If I click that option, the following empty function appears in the corresponding *.cs file:
private void MenuItem_Click(object sender, RoutedEventArgs e) { }
I realise that I probably need to add code to that empty method to do what I need to do, but so far I haven't found out how to do that.
There is also a 'Command' property in the properties list for the MenuItem. I also assume that that may play a part in sorting this out, but at this stage I don't know how.
Please let me know how this needs to be set up and big thanks in advance for any help.
Also, after searching elsewhere on stackoverflow, I found the basics for hiding/showing different windows. I've got that working OK, but it's not enough to resolve my problem.
Basically, in my real menu I have multiple menuItems and the clicking of each one needs to be dealt with in a different way. Therefore, in the MenuItem_Click function I need a way of identifying which MenuItem was actually clicked, so that I can respond in the correct way for each and every MenuItem click.
You can add click event for each menu item and add code for each method.inside this method you can add this code
Window1.Visibility = Visibility.Hidden;
window2.Visibility = Visibility.Visible;
you can use Command property when you need to bind methods in MVVM Pattern

Is it possible to make sure a context menu with multiple options open to the right by default [duplicate]

I have set up a simple sample menu.
<Menu>
<MenuItem Header="Top Menu">
<MenuItem Header="Item 1">
<MenuItem Header="Sub Menu 2" />
</MenuItem>
</MenuItem>
</Menu>
Sub Menu 2 is opening on the left instead of the expected right side of the menu. I would expect this behaviour if the menu was near the edge of the screen, but window is centered on the screen and not near any edge.
Is there a property that controls where a MenuItem opens on the screen?
Does this happen on other applications on your system as well?
There's a registry setting in the value MenuDropAlignment in the key HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows that controls it system-wide. You might just check to make sure that it is set to 0 instead of 1.
More info can be found in this article.

Is it possible to put a user control into a context menu

I don't know if this is possible but does anyone know if there is a way to put a user control inside a context menu, I have a dialog for adding items to a list but I would like when I click the add button for the control to be displayed in the context menu instead. (dont like having a lot of dialog's in my app).
Here is what I tried but it doesn't work:
<Button Grid.Column="2" Grid.Row="1" Content="Add Item" HorizontalAlignment="Right">
<Button.ContextMenu>
<ContextMenu>
<MenuItem>
<vm:MyControlView/>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
Is there a way to achieve this? (or something similar)
You could try this link, it looks like you might be able to extend ContextMenu like John Dunn did to meet your needs.

Datagrid + Mouse right click event

I have a datagrid and it contains the list of files from the folder. I want to display the default window files right click options in right click on the filename.
That is when ever i right click the filename in the datagrid, a default windows right click pop up should appear.
Can anyone help me to sort this out. The project is in C#.
Thank you.
You should be able to add a Context menu to your data grid. The Context Menu will allow you to add in all the regular Windows right click menu options. The example below only shows copy, cut and paste.
<my:DataGrid
ItemsSource="{Binding}"...>
<my:DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
</ContextMenu>
</my:DataGrid.ContextMenu>
</my:DataGrid>
You are able to add icons to the menu options as well if you'd like by writing the menu items more like this.
<MenuItem Command="Paste">
<MenuItem.Icon>
<Image Source="Images/paste.png" />
</MenuItem.Icon>
</MenuItem>

Categories

Resources