Click here to Skip to main content
13,863,412 members
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 1 Apr 2009
Licenced CC (Attr 3U)

Changing the Caret Colour in WPF

, 7 Apr 2009
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.


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


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
dyma7-Nov-12 22:56
memberdyma7-Nov-12 22:56 
GeneralMy vote of 1 Pin
thund3rstruck7-Apr-09 6:00
memberthund3rstruck7-Apr-09 6:00 
GeneralRe: My vote of 1 Pin
Nish Nishant7-Apr-09 7:59
sitebuilderNish Nishant7-Apr-09 7:59 
GeneralRe: My vote of 1 Pin
Derek Lakin7-Apr-09 8:02
memberDerek Lakin7-Apr-09 8:02 
GeneralRe: My vote of 1 Pin
Super Lloyd2-Jun-09 7:32
memberSuper Lloyd2-Jun-09 7:32 
GeneralRe: My vote of 1 Pin
Nish Nishant2-Jun-09 7:34
sitebuilderNish Nishant2-Jun-09 7:34 
GeneralRe: My vote of 1 Pin
Nish Nishant2-Jun-09 7:35
sitebuilderNish Nishant2-Jun-09 7:35 
GeneralRe: My vote of 1 Pin
Super Lloyd2-Jun-09 7:40
memberSuper Lloyd2-Jun-09 7:40 
QuestionMisleading? Pin
Jacquers7-Apr-09 5:27
memberJacquers7-Apr-09 5:27 
AnswerRe: Misleading? Pin
Derek Lakin7-Apr-09 5:34
memberDerek Lakin7-Apr-09 5:34 
GeneralRe: Misleading? Pin
Jacquers7-Apr-09 6:00
memberJacquers7-Apr-09 6: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 7 Apr 2009
Article Copyright 2009 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid