How can i set the caret position to a specific index in passwordbox in WPF

时间秒杀一切 提交于 2019-11-27 06:59:31

问题


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

Any help?


回答1:


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();



回答2:


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



来源:https://stackoverflow.com/questions/990311/how-can-i-set-the-caret-position-to-a-specific-index-in-passwordbox-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!