WPF 密码框插入符

发布于 2024-07-24 12:01:42 字数 118 浏览 5 评论 0原文

如何使用 WPF 虚拟键盘来使用密码框控件? 使用文本框控件,只需将插入符号移动到下一个文本位置就相当简单了; 密码框则不然,它不会暴露插入符号的位置。

我应该自己导出吗? 看起来酱汁很淡。

How do I use a passwordbox control with a virtual keyboard using WPF? With the textbox control, it's fairly simple to just move the caret to the next text position; not so with passwordbox, which doesn't expose the caret position.

Should I just derive my own? Seems like weak sauce.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-07-31 12:01:42

您可以尝试这样的方法来设置密码框中的选择:

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... or length of the password 
SetSelection( passwordBox1, 2, 0); 

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

上面的答案由 安德鲁·杰克逊,效果很好。

You can try something like this to set the selection in the PasswordBox:

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

After that, call it like this to set the cursor position:

// set the cursor position to 2... or length of the password 
SetSelection( passwordBox1, 2, 0); 

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

The above answer is provided by Andrew Jackson and it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文