WPF double click edit label

I have a label displaying some content. When a user double clicks the label, it should enable a user to change the content.

I did some research and found that it's usually very complex to achieve this simple effect. http://ludovic.chabant.com/devblog/2010/10/05/making-wpf-controls-double-clickable/

But all I need is a label (or textbox) that can be editable when double clicked. Is there a simpler way to do this?

Thanks!

You can create a textbox like:

TextBox editLbl = new TextBox();
//add it to the controls list to display it

and set all properties of the label to the textbox like location, size etc.

Set the keydown event and on pressing enter, hide the textbox and set the value to the label.

You can also set background color theme same as your form window has to look like you are editing the label.

Editable WPF Label Custom User Control, Simple and clean-cut label that user can edit text of during run-time (by double-​clicking) which I wrote because the examples I found were  When a user double clicks the label, it should enable a user to change the content. I did some research and found that it's usually very complex to achieve this simple effect. http://ludovic.chabant.com/devblog/2010/10/05/making-wpf-controls-double-clickable/ But all I need is a label (or textbox) that can be editable when double clicked.

I think you have to handle some events (setting up all in XAML code is not possible):

//Initially set Focusable to false
textBox1.Focusable = false;
textBox1.Cursor = Cursors.Arrow;

//FocusableChanged event handler for your textBox1
private void textBox1_FocusableChanged(object sender, 
                                       DependencyPropertyChangedEventArgs e){
  textBox1.Cursor = textBox1.Focusable ? Cursors.IBeam : Cursors.Arrow;
}
//MouseDoubleClick event handler for your textBox1
private void textBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e){
  textBox1.Focusable = true;
  textBox1.Focus();
  textBox1.CaretIndex = textBox1.Text.Length;
}
//LostFocus event handler for your textBox1
private void textBox1_LostFocus(object sender, RoutedEventArgs e){
  textBox1.Focusable = false;
}

The Label control, The binding is based on the name of the control, so if you change this name, you will also have to remember to change the binding. Using controls as Label  A relatively simple but pretty clean-cut approach to making editable labels. Background. Most solutions I found online for making editable labels in C# WPF add icons to the GUI to function with a mouse hover. I wanted one that the user can just double-click to edit, and press Enter or Tab to complete editing, and Escape to cancel editing.

WPF Programmer's Reference: Windows Presentation Foundation with , If you doubleclick on a control on the Toolbox, Visual Studio adds an instance of the control ComboBox, Grid, Image, Label, ListBox, RadioButton, Rectangle, StackPanel, Itcan edit C#, Visual Basic, XAML, images, icons, and even cursors. This makes sense given that the second click of a double-click is by definition always preceded by a single click, and in this case the call to MessageBox.Show in the PreviewMouseLeftButtonDown event handler is blocking the dispatcher thread once the first click has been recorded.

This way works fine for me. Fixed previous answer code in this thread (LostFocus is fired immediately after the double click).

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

public class DoubleClickTextBoxBehaviour : Behavior<TextBox>
{
    private long _timestamp;

    protected override void OnAttached()
    {
        AssociatedObject.Focusable = false;
        AssociatedObject.Cursor = Cursors.Arrow;

        AssociatedObject.MouseDoubleClick += AssociatedObjectOnMouseDoubleClick;
        AssociatedObject.LostFocus += AssociatedObjectOnLostFocus;
    }

    private void AssociatedObjectOnMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton != MouseButton.Left)
            return;

        if (AssociatedObject.Focusable)
            return;//fix an issue of selecting all text on double click

        AssociatedObject.Cursor = Cursors.IBeam;
        AssociatedObject.Focusable = true;
        AssociatedObject.Focus();
        AssociatedObject.CaretIndex = AssociatedObject.Text.Length;

        _timestamp = Stopwatch.GetTimestamp();
    }

    private void AssociatedObjectOnLostFocus(object sender, RoutedEventArgs e)
    {
        var delta = Stopwatch.GetTimestamp() - _timestamp;
        var timesp = new TimeSpan(delta);

        if (timesp.TotalSeconds < 1)
            return;

        AssociatedObject.Cursor = Cursors.Arrow;
        AssociatedObject.Focusable = false;
    }
}

Attach it to textbox:

<TextBox Text="{Binding MyText}">
    <i:Interaction.Behaviors>
        <behaviours:DoubleClickTextBoxBehaviour />
    </i:Interaction.Behaviors>
</TextBox>

If errors

Control.DoubleClick Event (System.Windows.Forms), Occurs when the control is double-clicked. public: event EventHandler ^ DoubleClick;. C# Copy. If a nonroot tree node was selected, it is put into an editable state, which allows the user to edit the node label. After the user stops editing the tree node label, the new label text is evaluated and saved. For this example, several characters are considered not valid in the label text.

.NET 4 Wrox PDF Bundle: Professional ASP.NET 4, Professional C# 4, , NET 4, Professional C# 4, VB 2010 Programmer's Ref, WPF Programmer's Ref, between the Window Designer and the XAML Editor to make one bigger and the at least) A Label Border, Button, CheckBox, ComboBox, Grid, Image, Label, Double-click on a file El "mm" . . . . , _ Harder to open it in the appropriate editor. In my WPF application , I am using ListView GridView, and I implemented a functionality that is associated to mouse double click. Is there a way, or a control that distinguish between the mouse double click and mouse click? I used a button, and implemented an event for mousedoubleclick, but the click event is still triggering. Thanks for help

How to Start Label Editing on Mouse Double Click, This article presents a custom ViewMode implementation to invoke the built-in label editor provided by class Graph2DView on mouse double clicks. Editing the string that is displayed with the edit-in-place control can be triggered with a timed 'double click'. This double click can be configured to occur in a certain time frame. There are 2 double dependency properties that can be setup to consume only those double clicks with a time frame that is larger than MinimumClickTime but smaller than MaximumClickTime .

Editing Labels - LabVIEW 2018 Help, Use the Labeling tool to click the label and highlight the text you want to edit. Double-click the label to highlight a word or triple-click the label to highlight the  - you need to manually change the namespace of "EditableTextBlock.xaml.cs" to Borgstrup.EditableTextBlock before the project compiles - the usual "slow double click" to begin editing is not implemented - and the usage in a databound context could be better (documented) Nevertheless is saved me time and therefore I vote 4

Comments
  • I'm pretty sure that Controls even does not exist in any context of WPF, maybe you thought the OP used winforms but not, s/he uses wpf.
  • I just devised some very similar code, but I'm finding the LostFocus is fired immediately after the double click event.