Click here to Skip to main content
Click here to Skip to main content

Caret for WPF User Controls

, 30 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple Caret for WPF User Controls

Introduction  

In this article I present a simple Caret which I have successfully used in a couple of my WPF projects. I hope that it can be useful for others too. 

Background 

WPF does not provide a Caret control for User Controls. There is a CaretElement, but it is internal to the framework and undocumented. The presented code can be easily adapted for WinForms as well.

Using the code  

Using the caret is pretty straightforward. You only have to create an instance of the Caret and add it as a child to your user control. In order to move the Caret around, you will have to change its Top and Left properties. You can change Caret's height using the CaretHeight property.

public class MyControl : UserControl 
{ 
    Caret caret = new Caret(); 
    public MyControl() 
    {
        AddChild(caret);
        caret.Top = 100;
        caret.Left = 100;
    }
}

And here is the Caret class:

public class Caret : FrameworkElement
{
    System.Threading.Timer timer;
    Point location;
    public double CaretHeight { get; set; }
    int blinkPeriod = 500;
    Pen pen = new Pen(Brushes.Black, 1);

    public static readonly DependencyProperty VisibleProperty = 
      DependencyProperty.Register("Visible", typeof(bool), 
      typeof(Caret), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

    public Caret()
    {
        pen.Freeze();
        CaretHeight = 18; 
        Visible = true;
        timer = new System.Threading.Timer(blinkCaret, null, 0, blinkPeriod);
    }

    protected override void OnRender(DrawingContext dc)
    {
        if (Visible)
        {
            dc.DrawLine(pen, location, new Point(Left, location.Y + CaretHeight));
        }
    }

    bool Visible
    {
        get
        {
            return (bool)GetValue(VisibleProperty);
        }
        set
        {
            SetValue(VisibleProperty, value);
        }
    }

    void blinkCaret(Object state)
    {
        Dispatcher.Invoke(new Action(delegate { Visible = !Visible; }));
    }
            
    public double Left
    {
        get { return location.X; }
        set
        {
            if (location.X != value)
            {
                location.X = Math.Floor(value) + .5; //to avoid WPF antialiasing
                if (Visible)
                {
                    Visible = false;
                }
            }
        }
    }

    public double Top
    {
        get { return location.Y; }
        set
        {
            if (location.Y != value)
            {
                location.Y = Math.Floor(value) + .5; //to avoid WPF antialiasing
                if (Visible)
                {
                    Visible = false;
                }
            }
        }
    }
}

License

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

Share

About the Author

Kashif_Imran
Software Developer MathiVersity
Pakistan Pakistan
I am a web developer mostly working in JavaScript. Formerly I used to be a C#/.Net developer. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.
 
I am founder of the Project MathiVersity, which aims to create free software, games and services related to mathematics and its allied disciplines.

Comments and Discussions

 
Questionhow to move the caret Pinmemberts8915-Aug-12 2:15 
AnswerRe: how to move the caret [modified] PinmemberKashif_Imran15-Aug-12 5:20 
GeneralMy vote of 4 PinmemberChristian Amado30-Jul-12 3:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150301.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Kashif_Imran
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid