How can i set the caret position to a specific ind

2020-02-06 08:10发布

I need to set the cursorposition inside the passwordbox explicitlyin WPF. i couldn't see the selectionstart property in passwordbox.

Any help?

2条回答
We Are One
2楼-- · 2020-02-06 08:30

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...
SetSelection( passwordBox1, 2, 0);

// focus the control to update the selection
passwordBox1.Focus();
查看更多
Animai°情兽
3楼-- · 2020-02-06 08:35

No, the API for PasswordBox does not expose a way to do this.

查看更多
登录 后发表回答