FukYouAll
FukYouAll

Reputation: 111

Style default TextBox ContextMenu in WPF

I've tried to style the ContextMenu of a customized TextBox in WPF adding <Setter Property="ContextMenu" Value="{StaticResource ProfessionalContextMenu}" /> to avoid this ugly appearance (the MenuItem Style is applied globally):

enter image description here

Using this ContextMenu style:

<Style x:Key="ProfessionalContextMenu" TargetType="{x:Type ContextMenu}">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="#FF494949"/>
    <Setter Property="Padding" Value="1"/>
</Style>

But it throws an exception (XamlParseException).

Upvotes: 0

Views: 1627

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

You trying assign Style to ContextMenu (<Setter Property="ContextMenu" Value="{StaticResource ProfessionalContextMenu}" />) and here is the problem probably.

Try using following code:

<TextBox>          
    <TextBox.ContextMenu>
        <ContextMenu Style="{StaticResource ProfessionalContextMenu}">
            <MenuItem Header="MenuItem1" />
            <MenuItem Header="MenuItem2" />
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

Upvotes: 2

Related Questions