我需要在WPF中显式设置密码框内的光标位置。我无法在密码框中看到selectionstart属性。有什么帮助吗?
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();
textBox.Select(textBox.Text.Length, 0)
。 - Marwan مروان