Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / XAML

Tips & Tricks: Styling Caret of a Silverlight TextBox Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (7 votes)
10 Oct 2010CPOL2 min read 21.2K   2   9
In this small tip, I will guide you through changing the Style of Caret of a Silverlight TextBox control.

Introduction

In this small tip, I will guide you through changing the Style of Caret of a Silverlight TextBox control. It is very simple and you can easily do it using Visual Studio or Expression Blend. Here is a screenshot of the same:

image

In general, styling the caret is not required. But in some cases, you may need to change it to give your UI the look & feel of your entire application.

What is a Caret?

Caret is a small line always blinking inside your TextBox control as shown in the above figure. The default color is always black. But you can change the color too as per your requirement.

How To Do It?

To do the styling for all the TextBox caret controls in your entire application, you need to add a Style for your TextBox in your App.xaml or in your theme page. Don’t set an xName to the style. If you set a name to the style, you have to explicitly set the style for each textbox using the Style property.

Setting a Style

Now in your style tag, add a setter property “CaretBrush”. This allows you to change the caret brush color of your textbox. Now set a value to it. You can use any color or color combination here. That means, you can set SolidColorBrush or GradientColorBrush here.

Setting a Solid Color

Let us first give a solid color for our TextBox caret. Here is the complete source code for it:

XML
<Style TargetType="TextBox">
    <Setter Property="CaretBrush">
        <Setter.Value>
            <SolidColorBrush Color="Red" />
        </Setter.Value>
    </Setter>
</Style>

When run, it will produce the following output:

image

Setting a Gradient Color

Now, let’s add a GradientColorBrush to it. We will use three different colors (i.e. Red, Yellow & Blue) with equal Offset. Here is the XAML code:

XML
<Style TargetType="TextBox">
    <Setter Property="CaretBrush">
        <Setter.Value>
            <LinearGradientBrush MappingMode="RelativeToBoundingBox"
                                 StartPoint="0,0" EndPoint="0,1">
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="Red" Offset="0" />
                    <GradientStop Color="Yellow" Offset="0.5" />
                    <GradientStop Color="Blue" Offset="1" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

This will produce the following output:

image

End Note

For better visibility, here is the scaled version of it:

image

Hope this will help you in future when you need to set a different color to your TextBox Caret. Feedback & suggestions are always appreciated.

Image 6
Image 7

Image 8

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralCaret shape Pin
voland8329-Dec-10 1:49
voland8329-Dec-10 1:49 
Is there any way to change a caret shape? I want to use rectangle caret which covers whole character instead of line which is on left on character.
GeneralMy vote of 5 Pin
Sandesh M Patil11-Oct-10 23:15
Sandesh M Patil11-Oct-10 23:15 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»12-Oct-10 0:06
professionalKunal Chowdhury «IN»12-Oct-10 0:06 
GeneralMy vote of 5 Pin
Hiren solanki11-Oct-10 22:46
Hiren solanki11-Oct-10 22:46 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»12-Oct-10 0:06
professionalKunal Chowdhury «IN»12-Oct-10 0:06 
GeneralMy vote of 5 Pin
Richard Waddell11-Oct-10 15:07
Richard Waddell11-Oct-10 15:07 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»11-Oct-10 22:35
professionalKunal Chowdhury «IN»11-Oct-10 22:35 
GeneralMy vote of 5 Pin
Abhijit Jana10-Oct-10 4:44
professionalAbhijit Jana10-Oct-10 4:44 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»10-Oct-10 4:45
professionalKunal Chowdhury «IN»10-Oct-10 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.