Question

Y at-il un moyen de cacher ou de déplacer le curseur de la PasswordBox?

Était-ce utile?

La solution

il n'y a aucun moyen propre de préciser dans SP1 ou .NET 3.5 précédent, la couleur d'une zone de texte WPF / PasswordBox caret.

Cependant, il existe un moyen de préciser (ou dans ce cas remove) qui caret de la vue (via un hack). La couleur de caret est la couleur inverse de la couleur de fond de TextBox / PasswordBox. Ainsi, vous pouvez faire « noir transparent » la couleur d'arrière-plan, ce qui tromperont le système en utilisant un caret blanc (non visible).

Le code est (simplement) comme suit:

<PasswordBox Background="#00000000" />

Pour plus d'informations sur cette question, s'il vous plaît consulter les liens suivants:

Notez que dans .NET 4.0 le Caret sera personnalisable.

Hope this helps!

Autres conseils

Vous pouvez essayer quelque chose comme ceci pour définir la sélection dans le PasswordBox:

private void SetSelection(PasswordBox passwordBox, int start, int length)
{ 
    passwordBox.GetType()
               .GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic)
               .Invoke(passwordBox, new object[] { start, length }); 
} 

Après cela, appeler comme celui-ci pour définir la position du curseur:

// set the cursor position to 2... or lenght of the password
SetSelection( passwordBox1, 2, 0); 

// focus the control to update the selection 
passwordBox1.Focus(); 

Pour obtenir la sélection de PasswordBox i utiliser ce 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;
}   

Testé à 4,0 .net, espoir qui fonctionne pour vous aussi.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top