如何在WPF中的密码框中将插入位置设置为特定索引

我需要在WPF中显式设置密码框内的cursorposition。 我在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(); 

不,PasswordBox的API没有公开这样做的方法。