TextBox Caret Styling

不羁的心 提交于 2019-11-30 17:17:52

The CaretElement is an internal sealed class and not possible to customize through a data template for example. At least, the caret brush is possible to change.

<TextBox Text="This is some random text" CaretBrush="Blue" />

If you want to have a linear gradient on the caret brush, this can be done.

<TextBox Text="This is some random text" FontSize="20">
        <TextBox.CaretBrush>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="Blue" Offset="0" />
                    <GradientStop Color="Red" Offset="1" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </TextBox.CaretBrush>

I tried using a Visual Brush also, but the caret is always being shown as a small vertical line.

Because the framework uses the inverse of the Background colour to set the colour of the caret, if you set the Background property to {x:Null}, then you will end up with the default black caret, which on a black background can be particularly difficult to see!

http://www.codeproject.com/Articles/34736/Changing-the-Caret-Colour-in-WPF

The simple solution to this problem is to actually specify a value for the Background property. In the case where you have a black background and want a white caret, you can set the value of the Background property to #00000000, which is completely transparent black (if that makes sense!). The framework appears to ignore the opacity component of the colour so you end up with a transparent background and a white caret!

And one more link: http://blogs.msdn.com/b/llobo/archive/2007/02/08/changing-caret-color-in-textbox.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!