Click here to Skip to main content
Email Password   helpLost your password?

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 to 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, then 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 overriding 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.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy vote of 1
thund3rstruck
6:00 7 Apr '09  
This is not an article and belongs on a message board or help forum.
GeneralRe: My vote of 1
Nishant Sivakumar
7:59 7 Apr '09  
thund3rstruck wrote:
This is not an article and belongs on a message board or help forum.


Dude! This is "not" an article. It's a blog entry featured via CP's new blog accumulation feature. D'Oh!


GeneralRe: My vote of 1
Derek Lakin
8:02 7 Apr '09  
Thanks Nish Smile
GeneralRe: My vote of 1
Super Lloyd
7:32 2 Jun '09  
How do you know it's not an article?
I'm probably missing the cues which differentiate article and, er.., blog entries

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.

GeneralRe: My vote of 1
Nishant Sivakumar
7:34 2 Jun '09  
Super Lloyd wrote:
How do you know it's not an article?
I'm probably missing the cues which differentiate article and, er.., blog entries


Previously it said so on the top left of the page. Now that seems to have been removed. But if you go to the author's article listing, you will see that this one shows up under blog-entries.

http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1190[^]


GeneralRe: My vote of 1
Nishant Sivakumar
7:35 2 Jun '09  
Also note the rating text that says "3 votes for this technical blog."

That's another clue.


GeneralRe: My vote of 1
Super Lloyd
7:40 2 Jun '09  
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.

GeneralMisleading?
Jacquers
5:27 7 Apr '09  
The title of the article is misleading, especially since you say: "I haven’t tried this yet, but I assume"
GeneralRe: Misleading?
Derek Lakin
5:34 7 Apr '09  
The article title is "Changing the Caret Colour in WPF". The section titled "The Solution" describes how to change the caret colour in WPF, so I think the title is pretty accurate Big Grin

The bit where I say "I haven’t tried this yet" is under the section titled "Taking it One Step Further". Do you still feel mislead?
GeneralRe: Misleading?
Jacquers
6: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

Anyways, good stuff on playing around with WPF and sharing your findings!


Last Updated 7 Apr 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010