简体   繁体   中英

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):

在此输入图像描述

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).

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM