Click here to Skip to main content
15,878,852 members
Articles / Desktop Programming / WPF

Changing the Caret Colour in WPF

Rate me:
Please Sign up or sign in to vote.
3.36/5 (4 votes)
7 Apr 2009CC (Attr 3U)1 min read 36.4K   3   11
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.
This article was originally posted at http://feeds2.feedburner.com/cloudstore

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
dyma7-Nov-12 21:56
dyma7-Nov-12 21:56 
GeneralMy vote of 1 Pin
thund3rstruck7-Apr-09 5:00
thund3rstruck7-Apr-09 5:00 
GeneralRe: My vote of 1 Pin
Nish Nishant7-Apr-09 6:59
sitebuilderNish Nishant7-Apr-09 6:59 
GeneralRe: My vote of 1 Pin
Derek Lakin7-Apr-09 7:02
Derek Lakin7-Apr-09 7:02 
GeneralRe: My vote of 1 Pin
Super Lloyd2-Jun-09 6:32
Super Lloyd2-Jun-09 6:32 
GeneralRe: My vote of 1 Pin
Nish Nishant2-Jun-09 6:34
sitebuilderNish Nishant2-Jun-09 6:34 
GeneralRe: My vote of 1 Pin
Nish Nishant2-Jun-09 6:35
sitebuilderNish Nishant2-Jun-09 6:35 
GeneralRe: My vote of 1 Pin
Super Lloyd2-Jun-09 6:40
Super Lloyd2-Jun-09 6:40 
ha ok, thanks.

That's an interesting feature hey! Smile | :)

A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

QuestionMisleading? Pin
Jacquers7-Apr-09 4:27
Jacquers7-Apr-09 4:27 
AnswerRe: Misleading? Pin
Derek Lakin7-Apr-09 4:34
Derek Lakin7-Apr-09 4:34 
GeneralRe: Misleading? Pin
Jacquers7-Apr-09 5:00
Jacquers7-Apr-09 5:00 

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

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