简体   繁体   中英

Apply DropShadowEffect to WPF Textbox text

How can I apply the DropShadowEffect on the content of a TextBox, as opposed to around the TextBox itself? I'd like the text to have the same effect as if I applied DropShadowEffect to a TextBlock.

<TextBox>
    <TextBox.Effect>
    <DropShadowEffect ShadowDepth="4"
                      Direction="330"
                      Color="Black"
                      Opacity="0.5"
                      BlurRadius="4"/>
    </TextBox.Effect>
</TextBox>

^This creates shadow around the entire box.

<TextBlock>
    <TextBlock.Effect>
    <DropShadowEffect ShadowDepth="4"
                      Direction="330"
                      Color="Black"
                      Opacity="0.5"
                      BlurRadius="4"/>
    </TextBlock.Effect>
</TextBlock>

^This is the desired look. (But for the TextBox text)

EDIT: Take home message is that shaders are applied to every rendered pixel of a control. If you want to apply it to only parts of it, either apply it on that level on that template, or don't render everything else.

By customizing the ControlTemplate of your TextBox , you can achive the desired effect:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="TextBox">
            <Grid x:Name="RootElement">
                <!-- Use your effects on the ContentPresenter here. -->
                <ContentPresenter Content="{TemplateBinding Padding}"/>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Instead you might want to remove the Border, Background and Focus rectangle from the textbox so you still have the TextBox functionality:

<TextBox Background="Transparent"
         BorderBrush="Transparent"
         BorderThickness="0"
         TextWrapping="Wrap">
    <TextBox.Effect>
        <DropShadowEffect ShadowDepth="4"
                          Direction="330"
                          Color="Black"
                          Opacity="0.5"
                          BlurRadius="4"
                           />
    </TextBox.Effect>
    <TextBox.FocusVisualStyle>
        <Style>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate/>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.FocusVisualStyle>
</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