Click here to Skip to main content
13,344,508 members (58,405 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 8 Dec 2008

Editable TextBlock in WPF for In-place Editing

, 10 Dec 2008
Rate this:
Please Sign up or sign in to vote.
An editable TextBlock component that allows for in-place editing of, e.g., items in a TreeView.
Sample Image


When renaming folders in the Windows Explorer folder tree, the TextBlock containing the name of the folder changes to a TextBox which allows for editing of the folder name inside of the tree. I couldn't find any complete examples, so I decided to make one myself. The EditableTextBlock control is based on the Annotating an Image in WPF article by Josh Smith.

Using the Code

Basically, the control is made of a TextBlock for displaying the text and a TextBox for editing. The IsInEditMode property described below determines which one is currently shown.

The EditableTextBlock control exposes four properties:

  • string Text - The editable text displayed to the user (note TextFormat below).
  • bool IsEditable - Whether or not the control is editable. If not, it behaves just as a regular TextBlock.
  • bool IsInEditMode - Whether or not the control is currently being edited.
  • string TextFormat - Used if the editable text should be surrounded by more text, like the root node in the screenshot above.

The item labeled 'Item 1.1' in the screenshot is defined in XAML as follows:

        <local:EditableTextBlock Text="Item 1.1" />

In order to let the user edit the contents of the control, you have to set the IsInEditMode property to true. For example, the demo application lets the user edit the selected item in the TreeView when the F2 key is pressed, by using the KeyDown event of the TreeView:

private void treeView1_KeyDown(object sender, KeyEventArgs e)
    if (e.Key == Key.F2)

private void SetCurrentItemInEditMode(bool EditMode)
    // Make sure that the SelectedItem is actually a TreeViewItem
    // and not null or something else
    if (treeView1.SelectedItem is TreeViewItem)
        TreeViewItem tvi = treeView1.SelectedItem as TreeViewItem;

        // Also make sure that the TreeViewItem
        // uses an EditableTextBlock as its header
        if (tvi.Header is EditableTextBlock)
            EditableTextBlock etb = tvi.Header as EditableTextBlock;

            // Finally make sure that we are
            // allowed to edit the TextBlock
            if (etb.IsEditable)
                etb.IsInEditMode = EditMode;

The control leaves edit mode when one of four things happen:

  • The control loses focus, e.g., when the user clicks on another control.
  • The user hits the Enter key, in which case the edited text is saved in the Text property.
  • The user hits the Escape key, in which case the original text, from before the editing started, is restored.
  • The IsInEditMode property is manually set to false.

The TextFormat Property

The TextFormat property uses the String.Format function to format the text, which means that the editable text is referenced by {0} inside a string. For example, the root node in the demo application is defined in XAML as follows:

        <local:EditableTextBlock Text="Root" TextFormat="Root Item '{0}'" />

If the TextFormat property is set to either the empty string (""), the string containing only {0} ("{0}"), or is not set at all, the control simply shows the string from the Text property.


  • December 8, 2008 - Created the article
  • December 8, 2008 - Updated source code


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


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt's not working.... Pin
Member 1245337321-Sep-16 21:53
memberMember 1245337321-Sep-16 21:53 
AnswerRe: It's not working.... Pin
Boumbles3-Nov-16 7:39
memberBoumbles3-Nov-16 7:39 
QuestionCould you update the namespace of the source code, so everyone can download and run? Pin
leiyangge12-Jan-14 22:26
memberleiyangge12-Jan-14 22:26 
GeneralMy vote of 2 Pin
Ivan Krivyakov20-Aug-13 14:21
memberIvan Krivyakov20-Aug-13 14:21 
QuestionRe: My vote of 2 Pin
Zoner10-Feb-14 0:34
memberZoner10-Feb-14 0:34 
AnswerProject Sample not Compiling - SOLUTION ! Pin
Member 799428912-Mar-13 10:31
memberMember 799428912-Mar-13 10:31 
GeneralRe: Project Sample not Compiling - SOLUTION ! Pin
Bogdan Maksimović1-Aug-13 4:03
memberBogdan Maksimović1-Aug-13 4:03 
GeneralMy vote of 4 Pin
anna.novikova19-Mar-12 8:58
memberanna.novikova19-Mar-12 8:58 
GeneralIn order to build the sample Pin
rencels17-Apr-11 7:12
memberrencels17-Apr-11 7:12 
QuestionEvents lost?? Pin
jamluke19-Nov-10 4:40
memberjamluke19-Nov-10 4:40 
GeneralPressing F2 to edit Pin
cmmello10-Nov-10 6:38
membercmmello10-Nov-10 6:38 
GeneralMy vote of 1 Pin
codddddd5-Nov-10 8:11
membercodddddd5-Nov-10 8:11 
GeneralMy vote of 1 Pin
PJBadenhorst3-May-10 1:06
memberPJBadenhorst3-May-10 1:06 
GeneralMy vote of 4 Pin
Christian Rodemeyer1-Feb-10 0:40
memberChristian Rodemeyer1-Feb-10 0:40 
GeneralRe: My vote of 4 Pin
bscaer14-Oct-10 8:32
memberbscaer14-Oct-10 8:32 
QuestionMultiline ? Pin
pbisiac27-Jan-10 1:58
memberpbisiac27-Jan-10 1:58 
GeneralMy vote of 1 Pin
Deni8415-Jan-10 4:07
memberDeni8415-Jan-10 4:07 
GeneralUsing HierarchicalDataTemplate Problem Pin
archmisha8-Dec-09 13:33
memberarchmisha8-Dec-09 13:33 
GeneralRe: Using HierarchicalDataTemplate Problem Pin
boetor14-Dec-09 22:58
memberboetor14-Dec-09 22:58 
QuestionRe: Using HierarchicalDataTemplate Problem Pin
piotri8530-Dec-09 7:20
memberpiotri8530-Dec-09 7:20 
AnswerRe: Using HierarchicalDataTemplate Problem Pin
jamluke19-Nov-10 4:16
memberjamluke19-Nov-10 4:16 
On event KeyDown

private void m_treeView_KeyDown(object sender, KeyEventArgs e)
            if (e.Key == Key.F2)
                EditableTextBlock etb = null;
                DataTemplate dt = m_treeView.ItemTemplate;
                StackPanel sp = dt.LoadContent() as StackPanel; --> To test null....
                UIElementCollection cool = sp.Children;
                for (int i = 0; i < cool.Count; i++)
                    object o = cool[i];
                    if (o as EditableTextBlock != null)
                    { etb = o as EditableTextBlock; break; }

                if (etb != null)
                    if (etb.IsEditable)
                        etb.EditMode = true

GeneralRe: Using HierarchicalDataTemplate Problem Pin
cohoman18-Mar-11 5:16
membercohoman18-Mar-11 5:16 
AnswerRe: Using HierarchicalDataTemplate Problem Pin
Member 809702021-Jul-11 8:23
memberMember 809702021-Jul-11 8:23 
AnswerRe: Using HierarchicalDataTemplate Problem Pin
andhome222-Jun-12 12:15
memberandhome222-Jun-12 12:15 
GeneralRe: Using HierarchicalDataTemplate Problem Pin
Sam Bomb4-Jul-12 2:15
memberSam Bomb4-Jul-12 2:15 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by Jesper Borgstrup
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid