WPF PasswordBox Caret
Asked Answered
S

4

5

Is there a way to hide or move the PasswordBox's caret?

Simmonds answered 1/6, 2009 at 17:9 Comment(1)
Any chance you might elaborate on that?Apeak
Z
5

In .NET 3.5 SP1 or previous, there is no clean way to specify the color of a WPF TextBox/PasswordBox caret.

However, there is a way to specify (or in this case remove) that caret from view (via a hack). The caret color is the inverse color of the TextBox/PasswordBox's background color. THus, you can make the background color "transparent black", which will fool the system into using a white caret (which is not visible).

The code is (simply) as follows:

<PasswordBox Background="#00000000" />

For further information on this issue, please check out the following links:

Note that in .NET 4.0 the Caret will be customizable.

Hope this helps!

Zebulun answered 2/6, 2009 at 6:17 Comment(2)
I know this is an old thread, but does anyone know anything about the 'customizable carat' in 4.0? I can't find itEke
Check the CaretBrush property on the TextBox: <TextBox CaretBrush="Aqua" />Zebulun
M
5

To Get the selection of Passwordbox i use this code:

private Selection GetSelection(PasswordBox pb)
{
    Selection result = new Selection();
    PropertyInfo infos = pb.GetType().GetProperty("Selection", BindingFlags.NonPublic | BindingFlags.Instance);

    object selection = infos.GetValue(pb, null);

    IEnumerable _textSegments = (IEnumerable)selection.GetType().BaseType.GetField("_textSegments", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(selection);

    object first_textSegments = _textSegments.Cast<object>().FirstOrDefault();

    object start = first_textSegments.GetType().GetProperty("Start", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments, null);
    result.start = (int) start.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(start, null);

    object end = first_textSegments.GetType().GetProperty("End", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments, null);
    result.length = (int)start.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(end, null) - result.start;

    return result;
}

struct Selection
{
    public int start;
    public int length;
}   

Tested at .net 4.0, hope that works for you too.

Moyna answered 7/5, 2014 at 16:28 Comment(2)
Sameer and klaus' answers helped implement an "eye" button that would hide/show password while leaving the cursor in the same position between plain text text box and hidden text password box.Furgeson
For reference, the "Set Cursor Position" can be found here; https://mcmap.net/q/470352/-how-can-i-set-the-caret-position-to-a-specific-index-in-passwordbox-in-wpfCyd
S
4

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... or lenght of the password
SetSelection( passwordBox1, 2, 0); 

// focus the control to update the selection 
passwordBox1.Focus(); 
Schematic answered 20/3, 2012 at 12:43 Comment(0)
E
0

A solution that solved this through .xaml on .NET 4.5.2:

   <PasswordBox Style="{DynamicResource PinEntry}">

Then on the style file under PinEntry style you can do the following:

    <Style x:Key="PinEntry" TargetType="{x:Type PasswordBox}">
       ...
       <Setter Property="CaretBrush" Value="Transparent"/>
       ...
    </Style>

This is actually my implementation using styles, you can modify the code to fit your needs. Hope it helps.

Erogenous answered 26/2, 2020 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.