wpf WPF双击编辑标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20368851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:09:36  来源:igfitidea点击:

WPF double click edit label

c#wpfwidget

提问by Ovilia

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/

我做了一些研究,发现实现这种简单的效果通常非常复杂。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!

谢谢!

采纳答案by Shaharyar

You can create a textboxlike:

你可以创建一个textbox像:

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

and set all properties of the label to the textboxlike location, sizeetc.

并设置标签的所有属性的textbox一样locationsize

Set the keydownevent and on pressing enter, hide the textboxand set the value to the label.

设置keydown事件并按回车键,隐藏textbox并将值设置为标签。

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

您还可以将background color主题设置为与表单窗口相同,看起来就像您正在编辑标签。

回答by King King

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

我认为您必须处理一些事件(不可能在 XAML 代码中设置所有事件):

//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;
}

回答by blindmeis

if you do non MVVM you can simply do the following

如果您使用非 MVVM,您可以简单地执行以下操作

  • create a Grid with your label and a hidden textbox
  • on label doubleclick event set textbox visibillity to visible
  • bind textbox text to label
  • on textbox lostfocus event set textbox visibillity to hidden
  • 使用您的标签和隐藏的文本框创建一个网格
  • 在标签双击事件上将文本框可见性设置为可见
  • 将文本框文本绑定到标签
  • 在文本框 lostfocus 事件上将文本框可见性设置为隐藏

回答by Stridemann

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

这种方式对我来说很好用。修复了此线程中先前的答案代码(双击后立即触发 LostFocus)。

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

如果出错