Context Menu Style not applied until right click - c#

I have a context menu attached to a button on a toolbar on one of my controls in WPF (.NET 4.0). The context menu has a style assigned to it in the XAML that defines the context menu. Left clicking on the button opens the button's context menu if it isn't opened already.
Here's the relevant XAML:
<Button x:Name="fileButton" Foreground="White" Margin="7, 0, -3, 0" VerticalAlignment="Stretch" MaxHeight="70" MaxWidth="78" MinHeight="55" MinWidth="62" Style="{DynamicResource ImageButton}" utils:WpfImageUtil.Image="{StaticResource fileButton}" Template="{DynamicResource GlassButton}" Content="File" Visibility="Visible" Click="fileButton_Click">
<Button.ContextMenu>
<ContextMenu Style="{DynamicResource ContextMenuStyle}">
<MenuItem x:Name="saveMenuItem" Header="Save" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="saveDrawingMenuItem" Header="Save Drawing" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="openMenuItem" Header="Open" Style="{DynamicResource MenuItemStyle}">
<MenuItem x:Name="openFromFile" Header="From File" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="openFromDesktop" Header="From Desktop" Style="{DynamicResource MenuItemStyle}" />
</MenuItem>
<MenuItem x:Name="iconsMenuItem" Header="Icons" ItemsSource="{Binding}" Style="{DynamicResource MenuItemStyle}"/>
<MenuItem x:Name="prefsMenuItem" Header="Preferences" Style="{DynamicResource MenuItemStyle}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
ContextMenuStyle is defined in a resource dictionary that is properly referenced.
When the context menu is opened with a left click, the style I have defined isn't applied to the menu, as shown below:
However, if the user right-clicks and opens the context menu the traditional way, the style is applied as expected:
Afterwards, left-clicking the button will show the style correctly:
I have been trying to figure this out for some time, but haven't been able to come up with any reason that this issue occurs. It seems like some kind of bug to me, but I'm not entirely sure. I also don't know what happens at the lower level when controls are right-clicked on that would cause the style to get applied correctly.

You should assign ContextMenu Style property in code (FindResource method msdn):
private void fileButton_Click(object sender, RoutedEventArgs e)
{
if (fileButton.ContextMenu.Style == null)
fileButton.ContextMenu.Style = this.FindResource("ContextMenuStyle") as Style;
fileButton.ContextMenu.IsOpen = true;
}
ContextMenu Overview (http://msdn.microsoft.com/en-US/library/ms742558.aspx)
A ContextMenu is attached to a specific control. The ContextMenu
element enables you to present users with a list of items that specify
commands or options that are associated with a particular control, for
example, a Button. Users right-click the control to make the menu
appear. ...
When you right-click on the control, style will be applied to the ContextMenu. So if you want to open ContextMenu in code, you should check if style is equal null and if it's true, you should assign appropriate style.

Related

xceed IntegerUpDown: shifted context menu values

I use Xceeed Wpf Toolkit and IntegerUpDown:
xmlns:XceedToolkit="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
<XceedToolkit:IntegerUpDown Grid.Column="1" Value="{Binding SelectedYear}" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
But, when i do right click on this control, take this:
If i make right click on anohter control like TextBox it is look normal.
Can you help me to fix it?
According to WPF Textbox's TextAlignment changes its default context menu item's alignment as well, you have to rebuild the context menu, since the default can't be changed.
<XceedToolkit:IntegerUpDown>
<XceedToolkit:IntegerUpDown.ContextMenu>
<ContextMenu TextBlock.TextAlignment="Left">
<MenuItem Command="ApplicationCommands.Copy" />
<MenuItem Command="ApplicationCommands.Cut" />
<MenuItem Command="ApplicationCommands.Paste" />
</ContextMenu>
</XceedToolkit:IntegerUpDown.ContextMenu>
</XceedToolkit:IntegerUpDown>
The issue is not specific to XceedToolkit:IntegerUpDown but applies to many controls with default context menu and TextAlignment property.
If you rather want to hack around with styles instead of replacing the context menu, your best bet for a target might be the Popup, since the context menu elements are internal classes somewhere, so creating a style for them won't work out of the box.
<XceedToolkit:IntegerUpDown>
<xt:IntegerUpDown.Resources>
<Style TargetType="Popup">
<Setter Property="TextBlock.TextAlignment" Value="Left"/>
</Style>
</xt:IntegerUpDown.Resources>
</xt:IntegerUpDown>

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.

How to open a form in same window in WPF application

I am learning c# WPF application.
I have a menu which has a dropdown list. In the dropdown there are 3 other items.
Now on clicking an item i want to display the form in the same window.
What i did is that i have created a new wpf form and navigated it from the previous page.
But now i want to open it in the same page itself.
Please help
XAML:
<Grid>
<DockPanel Height="43" HorizontalAlignment="Stretch" Name="dockPanel1"
VerticalAlignment="Top">
<Menu Height="47" Name="menu1" Width="Auto" DockPanel.Dock="Top"
VerticalAlignment="Top" HorizontalAlignment="Stretch" >
<MenuItem Header="_Entry Form" >
<MenuItem Header="_Student Details" Name="studentDetails" Click="studentDetails_Click_1"/>
<MenuItem Header="_Faculty Details" Name="facultyDetails" />
<Separator/>
<MenuItem Header="E_xit" Name="exit" />
</MenuItem>
</Menu>
</DockPanel>
CS
private void studentDetails_Click_1(object sender, RoutedEventArgs e)
{
// to navigate to a new page
Student std = new Student();
this.Close();
std.Show();
}
Now i donot want to navigate just display the student page in the same window
Firstly, you'll need to add XAML that represents the Student class, probably a bunch of TextBoxes, wrapped in some kind of container, like a Border, Grid, or StackPanel, and set the Visibility property of the container to Collapsed.
When the button is then clicked, you can populate the text boxes (or other UI elements) with the appropriate Student parameters, then set the container's Visibility property to Visible.
I would really recommend you check out the Model-View-ViewModel (MVVM) pattern, though. There is a bit of a learning curve, but it's well worth the investment.

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