Search code examples
wpftextboxcontextmenucontroltemplates

how to create style for default wpf contextmenu?


I have a requirement where to create a style for contextmenu which can apply automatically, i have tried every example which i found on web but nothing is working. i have tried the style which is narrated in the MSDN link is here: http://msdn.microsoft.com/en-us/library/ms744758(v=vs.85).aspx

I have used the below style but it is not working.

<Style x:Key="CStyle" TargetType="ContextMenu">
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="OverridesDefaultStyle"
                Value="True" />
        <Setter Property="Grid.IsSharedSizeScope"
                Value="true" />
        <Setter Property="HasDropShadow"
                Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContextMenu">
                    <Border Name="Border"
                            Background="Red"
                            BorderBrush="{StaticResource SolidBorderBrush}"
                            BorderThickness="1">
                        <StackPanel IsItemsHost="True"
                                    KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow"
                                 Value="true">
                            <Setter TargetName="Border"
                                    Property="Padding"
                                    Value="0,3,0,3" />
                            <Setter TargetName="Border"
                                    Property="CornerRadius"
                                    Value="4" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

can any one guide me to achieve this ?

I have tried to apply this style to the TextBoxStyle(see sample below)and when i run it and right click on content of textbox i see the below error:

"'System.Windows.Style' is not a valid value for property 'ContextMenu'." am i doing wrong anywhere in the below style?? please guide me.

Sample textbox style:

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseTextStyle}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="ContextMenu" Value="{StaticResource CStyle}" />
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>

Solution

  • EDIT: Based on revised question

    You are trying to set the value of ContextMenu to a Style. Change your style to the following:

        <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseTextStyle}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu Style="{StaticResource CStyle}">
                        <MenuItem Header="Cut" Command="Cut"/>
                        <MenuItem Header="Copy" Command="Copy"/>
                        <MenuItem Header="Paste" Command="Paste"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Style>