Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add dropshadoweffect to just the text of a textbox (programmatically)

Tags:

c#

wpf

textbox

I have a textbox that is programmatically added to a canvas at some point and I want all of the text to have a dropshadoweffect, but I don't want that effect applied to the borders of the textbox itself. How do I do this? Adding a dropshadoweffect to the textbox applies the effect to the borders of the box and "blurs" the text a little but that's not what I want and I cannot find any properties on the textbox that let me add an effect to the text alone. Do I really have to restyle the textbox or make my own template to achieve this??

Mind you this is a textbox, not a textblock (in which case I would just have copy/pasted from here)

like image 673
mtijn Avatar asked Aug 31 '11 12:08

mtijn


1 Answers

Update: Found a better way, you can skip the Border part if you apply the Effect directly to the ScrollViewer that encapsulates the text in the Template.

<TextBox Text="Shadow Text">
    <TextBox.Resources>
        <Style TargetType="ScrollViewer">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="4"
                                      Direction="330"
                                      Color="Black"
                                      Opacity="0.5"
                                      BlurRadius="4"/>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.Resources>
</TextBox>

Update 2: Missed the part of creating the TextBox in code. Here is the c# equivalent to the Xaml above

Setter effectSetter = new Setter();
effectSetter.Property = ScrollViewer.EffectProperty;
effectSetter.Value = new DropShadowEffect
{
    ShadowDepth = 4,
    Direction = 330,
    Color = Colors.Black,
    Opacity = 0.5,
    BlurRadius = 4
};
Style dropShadowScrollViewerStyle = new Style(typeof(ScrollViewer));
dropShadowScrollViewerStyle.Setters.Add(effectSetter);

TextBox dropShadowTextBox = new TextBox();
dropShadowTextBox.Text = "Shadow Text";
dropShadowTextBox.Foreground = Brushes.Teal;
dropShadowTextBox.FontSize = 40;
dropShadowTextBox.Margin = new Thickness(10);
dropShadowTextBox.Resources.Add(typeof(ScrollViewer), dropShadowScrollViewerStyle);

Good question, one idea is to make the Background and BorderBrush Transparent for the TextBox and place it in a Border

<Border BorderThickness="1"
        BorderBrush="#FF7F9DB9"
        SnapsToDevicePixels="True"
        UseLayoutRounding="True"
        Margin="10">
    <TextBox Text="Shadow Text"
             Foreground="Teal"
             FontSize="40"
             Background="Transparent"
             BorderBrush="Transparent">
        <TextBox.Effect>
            <DropShadowEffect ShadowDepth="4"
                        Direction="330"
                        Color="Black"
                        Opacity="0.5"
                        BlurRadius="4"/>
        </TextBox.Effect>
    </TextBox>
</Border>

Here is a comparison with a "normal" TextBox

enter image description here

like image 72
Fredrik Hedblad Avatar answered Oct 17 '22 16:10

Fredrik Hedblad