Click here to Skip to main content
Licence CC (Attr 3U)
First Posted 1 Apr 2009
Views 11,976
Bookmarked 2 times

Changing the Caret Colour in WPF

By Derek Lakin | 7 Apr 2009 | Technical Blog
Changing the Caret Colour in WPF
1 vote, 33.3%
1

2

3
1 vote, 33.3%
4
1 vote, 33.3%
5
3.20/5 - 3 votes
μ 3.20, σa 3.64 [?]

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

About the Author

Derek Lakin

Software Developer (Senior)

United Kingdom United Kingdom

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberthund3rstruck6:00 7 Apr '09  
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar7:59 7 Apr '09  
GeneralRe: My vote of 1 PinmemberDerek Lakin8:02 7 Apr '09  
GeneralRe: My vote of 1 PinmemberSuper Lloyd7:32 2 Jun '09  
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar7:34 2 Jun '09  
GeneralRe: My vote of 1 PinsitebuilderNishant Sivakumar7:35 2 Jun '09  
GeneralRe: My vote of 1 PinmemberSuper Lloyd7:40 2 Jun '09  
QuestionMisleading? PinmemberJacquers5:27 7 Apr '09  
AnswerRe: Misleading? PinmemberDerek Lakin5:34 7 Apr '09  
GeneralRe: Misleading? PinmemberJacquers6:00 7 Apr '09  
Maybe I was a bit too critical... I guess the title didn't say "Change the caret to any color" Poke tongue | ;-P
 
Anyways, good stuff on playing around with WPF and sharing your findings!

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.

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