WPF 密码框插入符

发布于 2024-07-23 11:10:13 字数 26 浏览 9 评论 0原文

有没有办法隐藏或移动密码框的插入符号?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光瘦了 2024-07-30 11:10:13

在 .NET 3.5 SP1 或更低版本中,没有干净的方法来指定 WPF TextBox/PasswordBox 插入符号的颜色。

但是,有一种方法可以从视图中指定(或在本例中删除)该插入符号(通过 hack)。 插入符号颜色是文本框/密码框背景颜色的反色。 因此,您可以将背景颜色设置为“透明黑色”,这将欺骗系统使用白色插入符(不可见)。

代码(简单)如下:

<PasswordBox Background="#00000000" />

有关此问题的更多信息,请查看以下链接:

请注意,在 .NET 4.0 中,插入符将是可自定义的。

希望这可以帮助!

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!

绮烟 2024-07-30 11:10:13

要选择密码框,我使用以下代码:

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;
}   

在.net 4.0 上测试,希望也适合您。

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.

趁年轻赶紧闹 2024-07-30 11:10:13

您可以尝试这样的方法来设置密码框中的选择:

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

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

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(); 
再可℃爱ぅ一点好了 2024-07-30 11:10:13

在.NET 4.5.2上通过.xaml解决这个问题的解决方案:

   <PasswordBox Style="{DynamicResource PinEntry}">

然后在PinEntry样式下的样式文件上您可以执行以下操作:

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

这实际上是我使用样式的实现,您可以修改代码以满足您的需求。
希望能帮助到你。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文