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

dee*_*pak 5 c# wpf textbox cursor passwordbox

我需要在WPF中显式设置密码框内的cursorposition.我在passwordbox中看不到selectionstart属性.

有帮助吗?

And*_*son 11

你可以尝试这样的东西来设置PasswordBox中的选择:

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

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

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

// focus the control to update the selection
passwordBox1.Focus();
Run Code Online (Sandbox Code Playgroud)