Change Caret Color in WinRT?

2019-05-19 21:34发布

So in WPF and Silverlight you could set the caret color for TextBox using TextBox.CaretBrush, but in WinRT it seems in all their wisdom they removed this property. So now if the TextBox is a dark color, since it defaults the caret color to black you can't see it, and if the background color of your TextBox is black the caret ceases to exist completely. I find this completely illogical, there has got to be a way to change the caret color, either pragmatically or something?

Does anyone have any ideas?

3条回答
萌系小妹纸
2楼-- · 2019-05-19 21:38

This is not possible. You need to switch your TextBox to light background when it has focus so the caret is visible.

查看更多
Explosion°爆炸
3楼-- · 2019-05-19 21:44

I got this working by overlaying a transparent canvas overtop of the TextBox, using SelectionStart and GetRectFromCharIndex to obtain coordinates, then on KeyDown, KeyUp, and MouseDown I set the coordinates of a Line and use a TranslateTransform to move it, works great and performance is fantastic!

Not sure why I didn't try this first, I've done Transforms in Silverlight and WPF many times prior. I guess the subtle differences in WinRt kind of threw me for a loop...

查看更多
smile是对你的礼貌
4楼-- · 2019-05-19 21:52

A little late to the party on this question but I ran into the same issue and found an easy work-around. On the Windows 8.1 RT preview I noticed that overriding the TextBoxForegroundThemeBrush changes the caret or cursor color for TextBox.

<SolidColorBrush x:Key="TextBoxForegroundThemeBrush" Color="#FFFFFFFF" />

Typically the cursor is always black, even when you override the Style and ControlTemplate. I happened to stumble on this by looking in the ThemeResources.xaml that Filip mentioned above. I'm not sure if this applies to pre-8.1 xaml, Microsoft seems to have cleaned a lot up in new SDK.

查看更多
登录 后发表回答