如何dropshadoweffect添加到文本框的只是文本(编程) [英] how to add dropshadoweffect to just the text of a textbox (programmatically)
问题描述
我有一个编程方式添加到在某些时候画布一个文本框,我想所有的文字有dropshadoweffect,但我不希望应用到文本框本身的边界的效果。我该怎么做呢?添加dropshadoweffect到文本框应用到盒子的边框和模糊文本有点效果,但是这不是我想要什么,我无法找到,让我一个人添加效果的文字文本框的属性。难道我真的要restyle文本框或自己做模板来实现这一目标?
你要知道,这是一个文本框,而不是一个文本块(在这种情况下,我只想从已经复制/粘贴这里)
更新:找到一个更好的方法,你可以跳过边框
部分如果您将效果
直接将的ScrollViewer
,封装在模板上的文字。
<预类=郎咸平的XML prettyprint-覆盖>
<文本框的文本=影子文本>
< TextBox.Resources>
<风格的TargetType =的ScrollViewer>
< setter属性=效果>
< Setter.Value>
< DropShadowEffect ShadowDepth =4
的方向=330
颜色=黑
透明度=0.5
BlurRadius =4/> ;
< /Setter.Value>
< /二传手>
< /样式和GT;
< /TextBox.Resources>
< /文本框>
更新2:未接创建代码文本框
。下面是C#等同于上面的XAML
二传手effectSetter =新二传手();
effectSetter.Property = ScrollViewer.EffectProperty;
effectSetter.Value =新DropShadowEffect
{
ShadowDepth = 4,
方向= 330,
颜色= Colors.Black,
不透明度= 0.5,
BlurRadius = 4
};
风格dropShadowScrollViewerStyle =新样式(typeof运算(的ScrollViewer));
dropShadowScrollViewerStyle.Setters.Add(effectSetter);
文本框dropShadowTextBox =新的TextBox();
dropShadowTextBox.Text =影子文本;
dropShadowTextBox.Foreground = Brushes.Teal;
dropShadowTextBox.FontSize = 40;
dropShadowTextBox.Margin =新厚度(10);
dropShadowTextBox.Resources.Add(typeof运算(的ScrollViewer),dropShadowScrollViewerStyle);
好问题,一个想法是让背景和BorderBrush透明的文本框
,并将其放置在边框
<预类=郎咸平的XML prettyprint-覆盖>
< BORDER了borderThickness =1
BorderBrush =#FF7F9DB9
SnapsToDevicePixels =真
UseLayoutRounding =真
保证金=10>
<文本框的文本=影子文本
前景=蒂尔
字号=40
背景=透明
BorderBrush =透明> ;
< TextBox.Effect>
< DropShadowEffect ShadowDepth =4
的方向=330
颜色=黑
透明度=0.5
BlurRadius =4/> ;
< /TextBox.Effect>
< /文本框>
< /边框>
下面是一个正常的文本框$ C $的比较C>
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)
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
这篇关于如何dropshadoweffect添加到文本框的只是文本(编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!