I am trying to modify the default style of the ContextMenu in WPF.
Normally you can creat a copy of the default in Expression Blend using the Edit Control Parts (Template) > Edit a Copy menu option. However I can't work out how to do this with a ContextMenu. Any idea how I can get the default style to modify?
I am trying to disable the left side of the context menu where the icons are normally shown.
Thanks!
Update: Maybe I wasn't clear about removing the icons. For example, if you have a context menu with no icons then the whole left side of the menu is wasted space. I would like to modify the default style of the context menu background to remove this. Simply I don't know how to access this default style.
For templates and styles that are not accessible through the Expression Interface (such as the ContextMenu template) you can use the following code to extract the template:
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
Using Writer As TextWriter = New StringWriter(sb)
System.Windows.Markup.XamlWriter.Save(ContextMenu.Template, Writer)
End Using
Debug.Write(sb.ToString)
Or in C#
var str = new StringBuilder();
using (var writer = new StringWriter(str))
XamlWriter.Save(ContextMenu.Template, writer);
Debug.Write(str);
Try this: (Put this code in your Resources part of your XAML) This should remove the icon strip from the context menu.
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" x:Name="Border" Background="White">
<StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I found the easy way to get the ContextMenu template in Blend:
That's it. Pick where you want the template/style to be put, and you're done.
Here's the markup I had:
<StackPanel x:Name="LayoutRoot">
<Button Content="Click for ContextMenu" Width="30" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.ContextMenu>
<ContextMenu Template="{DynamicResource ContextMenuControlTemplate1}" Style="{DynamicResource ContextMenuStyle1}">
<MenuItem Header="File"/>
<MenuItem Header="Edit"/>
<MenuItem Header="View"/>
<MenuItem Header="Recent Files"/>
<MenuItem Header="file1.txt"/>
<MenuItem Header="file2.txt"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
And the style/template I got:
<Style x:Key="ContextMenuStyle1" TargetType="{x:Type ContextMenu}">
<Setter Property="Background" Value="{DynamicResource MenuBackgroundBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource WindowBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Uid="Border_93">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0,0,5,5"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="4" Opacity="0.8" ShadowDepth="1"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Hope this helps. In usual MS thoroughness, the brushes in the default style aren't found. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With