【发布时间】:2009-06-13 08:26:56
【问题描述】:
我需要在 WPF 中明确设置密码框内的光标位置。我在密码框中看不到 selectionstart 属性。
有什么帮助吗?
【问题讨论】:
标签: c# wpf textbox passwordbox text-cursor
我需要在 WPF 中明确设置密码框内的光标位置。我在密码框中看不到 selectionstart 属性。
有什么帮助吗?
【问题讨论】:
标签: c# wpf textbox passwordbox text-cursor
您可以尝试这样的方法来设置 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 没有公开执行此操作的方法。
【讨论】:
textBox.Select(textBox.Text.Length, 0)