c# - 如何将插入符号位置设置为 WPF 密码框中的特定索引

标签 c# wpf textbox passwordbox text-cursor

我需要在 WPF 中明确设置密码框内的光标位置。我在 passwordbox 中看不到 selectionstart 属性。

有什么帮助吗?

最佳答案

您可以尝试像这样在 PasswordBox 中设置选择:

private void SetSelection(PasswordBox passwordBox, int start, int length) {
    passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(passwordBox, new object[] { start, length });
}

之后,像这样调用它来设置光标位置:

// set the cursor position to 2...
SetSelection( passwordBox1, 2, 0);

// focus the control to update the selection
passwordBox1.Focus();

关于c# - 如何将插入符号位置设置为 WPF 密码框中的特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/990311/

相关文章:

c# - 在 C# 中读取文本文件

c# - MVVM light 和运行时本地化更改

c# - 使用 Unity 和 MVVM 在 WPF 中保留用户凭据

c# - != 在 C# 中出现问题

c# - 在 WPF 文本框中粘贴事件

c# - 为什么在 C# 中启用多行的 TextBox 控件上禁用 CTRL+A 快捷方式?

c# - 强制客户端在调用同一个类的第二个方法之前调用一个方法是一种不好的做法吗?

javascript - 使用 ASP.Net Razor Pages 进行条件验证

c# - 默认样式不适用于子类

c# - 如何从头开始编程?