【问题标题】:WPF PasswordBox CaretWPF 密码框插入符号
【发布时间】:2009-06-01 17:09:53
【问题描述】:

有没有办法隐藏或移动 PasswordBox 的插入符号?

【问题讨论】:

  • 你能详细说明一下吗?

标签: wpf caret passwordbox


【解决方案1】:

在 .NET 3.5 SP1 或更早版本中,没有明确的方法来指定 WPF 文本框/密码框插入符号的颜色。

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

代码(简单)如下:

<PasswordBox Background="#00000000" />

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

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

希望这会有所帮助!

【讨论】:

  • 我知道这是一个旧线程,但有人知道 4.0 中的“可定制克拉”吗?我找不到它
  • 检查文本框上的 CaretBrush 属性:
  • 谢谢布拉德,我已经搜索过了。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    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 上测试,希望对你也有用。

    【讨论】:

    • Sameer 和 klaus 的回答帮助实现了一个“眼睛”按钮,该按钮可以隐藏/显示密码,同时将光标留在纯文本框和隐藏文本密码框之间的相同位置。
    【解决方案4】:

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

       <PasswordBox Style="{DynamicResource PinEntry}">
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多