Custom Caret “cheat” in WPF TextBox

I’ve seen people ask for custom Carets in the WPF TextBox, like in insert mode and you want to have a block caret or underscore. Some say it’s going to be possible/easier to do this in .NET 4.0, but until then, there are some simple cheats you can do to add it yourself, but it will need some tweaking. In this test I’ve put a Canvas on top of the TextBox, on which I draw the custom caret and move it when keys are pressed in the TextBox. If you absolutely need this behavior you can probably tweak it to work, but movement, drawing and all that depends on the font face and size.

CustomCaret

In my sample code, the custom caret is invisible to start with, and only drawn when in overwrite mode. The drawing is kind of jerky when using the arrow keys and home, end etc, but I think it can be fixed. One way would be to make the built in caret the same color as the TextBox background, like described by Lester in his blog post.

No Comments