[英]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.