TextBox 笔刷特效
开发工具与关键技术:Visual Studio 2017、WPF
撰写时间:2019年8月17日
TextBox 控件是我们开发过程中必不可少的组件,它可以使应用程序方便的与用户进行文字交互。在新WPF
4 中又为TextBox 添加了两种新笔刷特效:Selection 、Caret ,通过这两种特效使得TextBox
更加美观。
默认情况下TextBox 呈现出以下效果,字体为黑色、选区为蓝色、光标为黑色:
<TextBox
BorderBrush="Gray" BorderThickness="3"
Width="200" Height="30" Text="Hello World"/>
而使用了这些属性就发生了变化:
SelectionBrush 属性
- 字体颜色(Foreground)与选区颜色(SelectionBrush):
<TextBox
BorderBrush="Gray" BorderThickness="3"
Width="200" Height="30" Text="Hello World" Foreground="Red"
SelectionBrush="Green"/>
2. 选区透明度(SelectionOpacity):
<TextBox
BorderBrush="Gray" BorderThickness="3"
Width="200" Height="30" Text="Hello World"
Foreground="Red" SelectionBrush="Green" SelectionOpacity="1"/>
- 渐变选区效果:
<TextBox
BorderBrush="Gray" BorderThickness="3"
Height="20" Text="Hello World"
Margin="247,321,247,79">
<TextBox.SelectionBrush>
<LinearGradientBrush
StartPoint="0,0" EndPoint="1,1">
<GradientStop
Color="Red" Offset="0" />
<GradientStop
Color="Yellow" Offset="0.2" />
<GradientStop
Color="Blue" Offset="0.4" />
<GradientStop
Color="Green" Offset="0.6" />
<GradientStop
Color="Orchid" Offset="0.8" />
<GradientStop
Color="Pink" Offset="1" />
</LinearGradientBrush>
</TextBox.SelectionBrush>
</TextBox>
CaretBrush 属性
Caret笔刷可以用来修改光标颜色:
<TextBox
BorderBrush="Gray" BorderThickness="3"
Width="200" Height="30" Text="Hello World" CaretBrush="Red" />