28

I've found a few things on setting CaretBrushes in WPF4, but has anybody actually ever changed the caret itself?

What I'd like to do is use the OVERWRITE caret in INSERT mode. I've seen a hack from .Net 3.5 times, but it is unperformant and lacks behind actual cursor movement...

It would be great if the Caret had a Template - That would be consistent with the whole WPF idea...

Any advice?

1

2 Answers 2

56

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.

13

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!

Conclusion: You have to specify the background property with opacity to control the caret styling.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.