Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / WPF
Article

Editable TextBlock in WPF for In-place Editing

Rate me:
Please Sign up or sign in to vote.
4.33/5 (30 votes)
10 Dec 2008CPOL2 min read 189.1K   8.3K   51   42
An editable TextBlock component that allows for in-place editing of, e.g., items in a TreeView.
Sample Image

Introduction

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:

XML
<TreeViewItem>
    <TreeViewItem.Header>
        <local:EditableTextBlock Text="Item 1.1" />
    </TreeViewItem.Header>
</TreeViewItem>

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:

C#
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F2)
        SetCurrentItemInEditMode(true);
}

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:

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

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.

History

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

License

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


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Using HierarchicalDataTemplate Problem Pin
Member 809702021-Jul-11 7:23
Member 809702021-Jul-11 7:23 
AnswerRe: Using HierarchicalDataTemplate Problem Pin
andhome222-Jun-12 11:15
andhome222-Jun-12 11:15 
GeneralRe: Using HierarchicalDataTemplate Problem Pin
Sam Bomb4-Jul-12 1:15
Sam Bomb4-Jul-12 1:15 
GeneralRe: Using HierarchicalDataTemplate Problem Pin
GANESH KUMAR K22-Sep-16 2:00
GANESH KUMAR K22-Sep-16 2:00 
GeneralBinding IsInEditMode Pin
Patrick Goergen29-Sep-09 6:34
Patrick Goergen29-Sep-09 6:34 
GeneralRe: Binding IsInEditMode Pin
eliprand28-Jan-10 7:09
eliprand28-Jan-10 7:09 
Generalmy vote is 5 Pin
Kunal Chowdhury «IN»4-Aug-09 1:24
professionalKunal Chowdhury «IN»4-Aug-09 1:24 
QuestionBinding to collection lost after edit item Pin
Jeff4331-Jul-09 14:17
Jeff4331-Jul-09 14:17 
My application creates a hierarchical ObservableCollection<object> which is bound to my TreeView. The TreeViewItems are styled and arranged as follows:

<HierarchicalDataTemplate DataType="{x:Type Folder}"<br />
 ItemsSource="{Binding Path=Items}"><br />
   <uc:EditableTextBlock Text="{Binding Path=Name}" FontSize="12" Foreground="Blue"/><br />
</HierarchicalDataTemplate>


What works:
- Display TreeViewItems
- Drag and Drop TreeViewItems
- Edit TreeViewItems
- Modify my ObservableCollection and TreeViewItems reflect the change automatically.

What isn't working:
- After Editing a TreeViewItem, the change is not propagated up to the ObservableCollection.
- At this point, if the Collection is modified, it skips the TreeViewItem that was modified.

Its like the binding was lost when the TreeViewItem was edited which would explain both problems. Could this be what is happening?

Any thoughts on what might be the problem and/or solution?

Thanks,
AnswerRe: Binding to collection lost after edit item Pin
Jeff439-Aug-09 8:18
Jeff439-Aug-09 8:18 
GeneralDep. Properties Pin
mscholz17-Jun-09 20:21
mscholz17-Jun-09 20:21 
GeneralClick-to-Edit modification PinPopular
Ron Dunant29-Mar-09 17:36
Ron Dunant29-Mar-09 17:36 
GeneralRe: Click-to-Edit modification Pin
jholland878-Sep-11 10:22
jholland878-Sep-11 10:22 
GeneralBinding Mode Pin
Ed Havelaar20-Jan-09 15:37
Ed Havelaar20-Jan-09 15:37 
GeneralNot able to run Application Pin
Member 142043910-Jan-09 21:36
Member 142043910-Jan-09 21:36 
GeneralRe: Not able to run Application PinPopular
west18824-Jan-09 16:41
west18824-Jan-09 16:41 
GeneralRe: Not able to run Application Pin
Mai Minh Tien7-Feb-22 19:41
Mai Minh Tien7-Feb-22 19:41 
GeneralBuilding sample Pin
Rod Cullison24-Dec-08 4:25
Rod Cullison24-Dec-08 4:25 
QuestionHow do you bind to the Text property Pin
masontwo8-Dec-08 8:37
masontwo8-Dec-08 8:37 
AnswerRe: How do you bind to the Text property Pin
Jesper Borgstrup8-Dec-08 9:03
Jesper Borgstrup8-Dec-08 9:03 
GeneralRe: How do you bind to the Text property Pin
Daggs8-Dec-08 15:20
Daggs8-Dec-08 15:20 

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.