Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Changing the Caret Colour in WPF

, 7 Apr 2009 CC (Attr 3U)
Rate this:
Please Sign up or sign in to vote.
Changing the Caret Colour in WPF

In WPF, you don't have direct control over the colour of the caret in a TextBox or a RichTextBox. What the framework does is use the inverse of the colour that you choose for the Background property. I've seen a few examples of styles that explicitly set the Background property to {x:Null} to completely remove the background from the TextBox, which enables you to use the same style when the TextBox is used on top of different backgrounds that you want to show through.

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!

The Solution

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!

Taking it One Step Further

I haven't tried this yet, but I assume that you can take this approach one step further to customise the colour of your caret by setting the Background to different colours with an opacity value of zero. This would then be a simpler solution that completely overrides the style as described by Lester in his Changing caret color in (Rich)TextBox post.

This work is licensed under a Creative Commons Attribution By license.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License

Share

About the Author

Derek Lakin
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberdyma7-Nov-12 22:56 
GeneralMy vote of 1 Pinmemberthund3rstruck7-Apr-09 6:00 
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar7-Apr-09 7:59 
GeneralRe: My vote of 1 PinmemberDerek Lakin7-Apr-09 8:02 
GeneralRe: My vote of 1 PinmemberSuper Lloyd2-Jun-09 7:32 
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar2-Jun-09 7:34 
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar2-Jun-09 7:35 
GeneralRe: My vote of 1 PinmemberSuper Lloyd2-Jun-09 7:40 
QuestionMisleading? PinmemberJacquers7-Apr-09 5:27 
AnswerRe: Misleading? PinmemberDerek Lakin7-Apr-09 5:34 
GeneralRe: Misleading? PinmemberJacquers7-Apr-09 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 7 Apr 2009
Article Copyright 2009 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid