[英]how to add dropshadoweffect to just the text of a textbox (programmatically)
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. 向文本框添加dropshadoweffect会将效果应用于框的边框,并稍微“模糊”文本,但这不是我想要的,并且我无法在文本框上找到任何可以单独为文本添加效果的属性。 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 ) 请注意,这是一个文本框,而不是文本块(在这种情况下,我将只从此处复制/粘贴)
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. 更新:找到了一种更好的方法,如果将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>
Update 2: Missed the part of creating the TextBox
in code. 更新2:缺少在代码中创建TextBox
的部分。 Here is the c# equivalent to the Xaml above 这是与上述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);
Good question, one idea is to make the Background and BorderBrush Transparent for the TextBox
and place it in a Border
好问题,一个想法是使TextBox
的Background和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>
Here is a comparison with a "normal" TextBox
这是与“普通” TextBox
的比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.