如何将 dropshadoweffect 添加到文本框的文本(以编程方式)

发布于 2024-12-02 07:23:27 字数 319 浏览 0 评论 0原文

我有一个在某个时刻以编程方式添加到画布的文本框,我希望所有文本都具有投影效果,但我不希望将该效果应用于文本框本身的边框。我该怎么做?向文本框添加 dropshadoweffect 会将效果应用到框的边框并稍微“模糊”文本,但这不是我想要的,并且我在文本框中找不到任何可让我单独向文本添加效果的属性。我真的必须重新设计文本框或制作自己的模板才能实现此目的吗?

请注意,这是一个文本框,而不是文本块(在这种情况下,我只需从 此处

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-12-09 07:23:27

更新:找到了更好的方法,如果将Effect直接应用于ScrollViewer,则可以跳过Border部分将文本封装在模板中。

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

更新2:错过了在代码中创建TextBox的部分。这是与上面的 Xaml 等效的 C#

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

好问题,一个想法是使 TextBox 的背景和 BorderBrush 透明,并将其放置在 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>

这是与一个“正常”TextBox

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文