zoukankan      html  css  js  c++  java
  • WPF 解决”文本框粘贴字符串有换行时被截取“的问题

    public class TextBoxPasteHelper:DependencyObject
        {
            public static bool GetCanPasteNewLine(DependencyObject d)
            {
                return (bool)d.GetValue(CanPasteNewLineProperty);
            }
    
            public static void SetCanPasteNewLine(DependencyObject d,bool value)
            {
                d.SetValue(CanPasteNewLineProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for CanPasteNewLine.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CanPasteNewLineProperty =
                DependencyProperty.RegisterAttached("CanPasteNewLine", typeof(bool), typeof(TextBoxPasteHelper), new PropertyMetadata(default(bool),OnCanPasteNewLineChanged));
    
            private static void OnCanPasteNewLineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var currentTextBox = d as TextBox;
                if(currentTextBox != null)
                {
                    var cmd = new CommandBinding();
                    cmd.Command = ApplicationCommands.Paste;
                    cmd.Executed += Cmd_Executed;
                    currentTextBox.CommandBindings.Add(cmd);
                    var keyBinding = new KeyBinding();
                    keyBinding.Key = Key.V;
                    keyBinding.Modifiers = ModifierKeys.Control;
                    keyBinding.Command = ApplicationCommands.Paste;
                    currentTextBox.InputBindings.Add(keyBinding);
                }
            }
    
            private static void Cmd_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                string copiedStr = Clipboard.GetText();
                if (string.IsNullOrEmpty(copiedStr))
                    return;
    
                var currentTB = sender as TextBox;
                if (currentTB != null)
                {
                    if (currentTB.SelectedText == currentTB.Text)
                    {
                        currentTB.Text = copiedStr;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(currentTB.SelectedText))
                        {
                            currentTB.SelectedText = copiedStr;
                        }
                        else
                        {
                            StringBuilder currentStr = new StringBuilder(currentTB.Text);
                            currentStr.Insert(currentTB.CaretIndex, copiedStr);
                            currentTB.Text = currentStr.ToString();
                        }
                    }
                }
            }
        }
    使用:
    <TextBox attached:TextBoxPasteHelper.CanPasteNewLine="True"/>
  • 相关阅读:
    C# 控制台程序如何输出Messagebox
    C# VS如何整个项目中查找字符串
    C# .NET using ManagementObjectSearcher提示缺少引用怎么办
    [React] Write a Custom State Hook in React
    [GCP] Goolge compute Engine
    [Kubernetes] Defining a Pod with YAML
    [Kubernetes] Kubectl and Pod
    [Schematics] 1. Copy and Manipulate Template
    [Schematics] 0. Schematics "Hello World"
    [RxJS] Subject asObservable() method
  • 原文地址:https://www.cnblogs.com/JqkAman/p/14138155.html
Copyright © 2011-2022 走看看