Inside the UserControl's constructor, I subscribe to three TextBox events ( SelectionChanged, LostFocus, and GotFous). += (sender, e) => Caret.Visibility = Visibility.Visible += (sender, e) => Caret.Visibility = Visibility.Collapsed / /// Initializes a new instance of the class. I've also added a Storyboard to the Border to simulate the blinking effect of a 'built-in' Caret. Even though this can be achieved in various ways, I felt a Storyboard was a bit easier as it can be implemented in pure XAML. ![]() This is what will be moving/updating across the Canvas. Border - The Border is the actual custom Caret. ![]() Since it lives inside a Grid that is overlapping the actual TextBox, the custom Caret will appear as if it was part of the TextBox itself. Canvas - The Canvas is the area that the custom Caret will move on.This is required so that the 'built-in' Caret is not displayed and distracting the user from the custom Caret. The TextBox will also provide us with the 'built-in' Caret's position. Notice the ' CaretBrush' of the TextBox is set to Transparent. We'll be referencing this TextBox to get notified when the 'built-in' Caret moves so that the position of our custom Caret can move as well. TextBox - The TextBox where the 'built-in' Caret lives.This is important because we want our custom Caret to appear as if it was part of the actual TextBox. A Grid is used so that the TextBox, Canvas, and Border overlap seamlessly. Grid - This is the parent "container" of the TextBox and the other controls.Using a style, the CaretBrush property can be set to specify the color (in this case blue).Īs you can see, I've added the following 4 controls to a UserControl. ![]() The CaretBrush allows for easy color customization. NET 4.0, Microsoft has provided the capability to customize the Caret's color by providing the CaretBrush property of a TextBox control. Even-though the thickness can be changed through the Windows Accessibility settings, it is an OS wide change, and not always desired if you want it to be application dependent. BackgroundĪ Caret is the vertical 'blinking' line that represents the current cursor position of a control that accepts text input.īy default, the Window's Caret is only 1-pixel wide and the shape is currently not modifiable using. It provides information on how to change the color, as well as a technique that will allow you to create your own custom Caret - providing you freedom with shape and size. This article describes the different ways a WPF TextBox Caret can be customized.
0 Comments
Leave a Reply. |