简体   繁体   English

如何在 WPF、C# 中通过 CTRL+V 粘贴

[英]How can I paste by CTRL+V in WPF, C#

I have C# WPF project, I have 4 cells and I tried paste some code to those cells(like PIN code), but when i clicked CTRL+V , its copied first digit only to first cell.我有 C# WPF 项目,我有 4 个单元格,我尝试将一些代码粘贴到这些单元格(如 PIN 码),但是当我单击 CTRL+V 时,它的第一个数字仅复制到第一个单元格。 How can I resolve that?我该如何解决?

.xaml file .xaml 文件

<StackPanel Style="{DynamicResource HorizontalPanel}" >
                    <Menu>
                        <MenuItem Command="ApplicationCommands.Paste" />
                    </Menu>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit1Text}" Name="Digit1" TextChanged="Digit1_TextChanged" />
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit2Text}" Name="Digit2" TextChanged="Digit2_TextChanged"/>
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit3Text}" Name="Digit3" TextChanged="Digit3_TextChanged"/>
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit4Text}" Name="Digit4" TextChanged="Digit4_TextChanged"/>
                    </Border>
</StackPanel>

.xaml.cs file: .xaml.cs 文件:

}

        public PasswordScreen(object object1, object object2, ConnectorAction connectorAction)
        {
            InitializeComponent();
            Digit1.Focus();            
            Object1 = object1;
            Object2 = object2;
            ConnectorAction = connectorAction;          
            MenuItem pasteMenuItem = new MenuItem();          
            pasteMenuItem.Command = ApplicationCommands.Paste;                      
        }

private void Digit1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Digit1.Text.Length == 1)
            {
                Digit2.Focus();
            }          
        }

same for Digit2_TextChanged,Digit3_TextChanged,Digit4_TextChanged Digit2_TextChanged、Digit3_TextChanged、Digit4_TextChanged 相同

I don't understand how I catch here the pin - code and paste the numbers each number on different cell?我不明白我是如何在这里捕捉密码并将每个数字粘贴到不同单元格上的? emphasized text强调文本

If i understood that correctly you want to paste the 4 digit pin code and every digit in one TextBox.如果我理解正确,您想将 4 位密码和每个数字粘贴到一个 TextBox 中。

One way could be to handle it in the text changed of the TextBox (example for first text box, for other textbox modification needed if all text boxes should support the splitting):一种方法是在 TextBox 更改的文本中处理它(例如第一个文本框,如果所有文本框都支持拆分,则需要其他文本框修改):

private void Digit1_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (Digit1.Text.Length <= 1)
            return;
        
        string text = Digit1.Text;
        Digit2.Text = text[1].ToString();
        if (text.Length > 2)
            Digit3.Text = text[2].ToString();
        if (text.Length > 3)
            Digit4.Text = text[3].ToString();

        Digit1.Text = text[0].ToString();
    }

Or write a own command for pasting which could be invoked through a button click or a shortcut:或者编写一个自己的粘贴命令,可以通过单击按钮或快捷方式调用:

private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (!Clipboard.ContainsText())
            return;

        string clipboardText = Clipboard.GetText();

        if (string.IsNullOrEmpty(clipboardText))
            return;

        int length = clipboardText.Length;
        if (length > 0)
            Digit1.Text = clipboardText[0].ToString();
        if (length > 1)
            Digit2.Text = clipboardText[1].ToString();
        if (length > 2)
            Digit3.Text = clipboardText[2].ToString();
        if (length > 3)
            Digit4.Text = clipboardText[3].ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 模拟 CTRL+V 击键(粘贴) - How simulate CTRL+V keystrokes (paste) using C# 如何使用Ctrl + v在文本框中启用粘贴 - How do I enable paste in textbox with Ctrl+v 有没有更简单的方法在C#控制台应用程序中使用Windows ctrl + v(粘贴)功能? - Is there an easier way to use Windows ctrl+v (paste) functionality in a C# Console application? 如何在空列表框中使用 CTRL+V 启用粘贴? - How to enable paste using CTRL+V in empty listbox? 在Excel中禁用复制(Ctrl + C),粘贴(Ctrl + V),剪切(Ctrl + X) - Disable copy(ctrl+c), paste(ctrl+v),cut(ctrl+x) in excel 允许在文本框中仅浮动(和负浮动),select 全部,复制并粘贴(Ctrl+A、Ctrl+C、Ctrl+V) - Allow in textbox only float (and negative float), select all, copy and paste (Ctrl+A, Ctrl+C, Ctrl+V) 如何在WPF / C#中捕获CTRL-BACKSPACE? - How can I capture CTRL-BACKSPACE in WPF/C#? VSTO Word AddIn:通过Ctrl+V捕获粘贴操作 - VSTO Word AddIn : Capture Paste operation by Ctrl+V 在目标应用程序中使用 C# 结果“v”模拟键盘敲击 Ctrl+V - Simulate keyboard stroke Ctrl+V using C# results “v” in target application C#全局覆盖使用全局键盘钩子的CTRL + V命令 - C# Global override CTRL+V command using global keyboard hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM