Skip to content

Instantly share code, notes, and snippets.

@punker76
Created October 12, 2017 21:36
Show Gist options
  • Save punker76/ed9fafcdb56da179f255db52f3787052 to your computer and use it in GitHub Desktop.
Save punker76/ed9fafcdb56da179f255db52f3787052 to your computer and use it in GitHub Desktop.
GetPasswordBoxCaretPostion
private int GetPasswordBoxCaretPostion(PasswordBox passwordBox)
{
var infos = passwordBox?.GetType()?.GetProperty("Selection", BindingFlags.NonPublic | BindingFlags.Instance);
var selection = infos?.GetValue(passwordBox, null) as TextSelection;
var tTextRange = selection?.GetType()?.GetInterfaces()?.Where(x => x.Name == "ITextRange")?.FirstOrDefault();
var oStart = tTextRange?.GetProperty("Start")?.GetGetMethod()?.Invoke(selection, null);
var value = oStart?.GetType()?.GetProperty("Offset", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(oStart, null) as int?;
var caretPosition = value.GetValueOrDefault(0);
return caretPosition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment