如何在 wpf 密码框设置一些默认文本? [复制]

2024-05-03

可能的重复:
WPF 中的水印文本框 https://stackoverflow.com/questions/833943/watermark-textbox-in-wpf

我可以知道如何在 WPF 的密码框中放入一些默认文本吗? 我以 MSN Messenger 为例。默认情况下,密码字段将在密码框中显示“密码”。当我单击并输入密码框时,“密码”文本将消失,并由点代替。


您必须创建一个自定义控件才能做到这一点。

XAML:

<Window x:Class="WpfTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfTest="clr-namespace:WpfTest" 
    Title="Password Box Sample" Height="300" Width="300"> 
  <Window.Resources> 
    <Style x:Key="{x:Type PasswordBox}" 
        TargetType="{x:Type PasswordBox}"> 
      <Setter Property="WpfTest:PasswordBoxMonitor.IsMonitoring" 
              Value="True"/> 
      <Setter Property="Template"> 
        <Setter.Value> 
          <ControlTemplate TargetType="{x:Type PasswordBox}"> 
            <Border Name="Bd" 
                    Background="{TemplateBinding Background}" 
                    BorderThickness="{TemplateBinding BorderThickness}" 
                    BorderBrush="{TemplateBinding BorderBrush}" 
                    SnapsToDevicePixels="true"> 
              <Grid> 
                <ScrollViewer x:Name="PART_ContentHost" 
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
                <TextBlock Text="Please enter your password"  
                           Margin="4, 2, 0, 0" 
                           Foreground="Gray"  
                           Visibility="Collapsed" 
                           Name="txtPrompt" /> 
              </Grid> 
            </Border> 
            <ControlTemplate.Triggers> 
              <Trigger Property="IsEnabled" 
                                                                         Value="false"> 
                <Setter TargetName="Bd" 
                                                                                Property="Background" 
                                                                                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
                <Setter Property="Foreground" 
                                                                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
              </Trigger> 
              <Trigger Property="WpfTest:PasswordBoxMonitor.PasswordLength" Value="0"> 
                <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/> 
              </Trigger> 
            </ControlTemplate.Triggers> 
          </ControlTemplate> 
        </Setter.Value> 
      </Setter> 
    </Style> 
  </Window.Resources> 
  <Grid> 
    <PasswordBox VerticalAlignment="Top"/> 
  </Grid> 
</Window> 

C# code:

using System.Windows; 
using System.Windows.Controls; 

namespace WpfTest 
{ 
    public partial class Window1 : Window 
    { 
        public Window1() 
        { 
                InitializeComponent(); 
        } 

    } 

  public class PasswordBoxMonitor : DependencyObject 
  { 
    public static bool GetIsMonitoring(DependencyObject obj) 
    { 
      return (bool)obj.GetValue(IsMonitoringProperty); 
    } 

    public static void SetIsMonitoring(DependencyObject obj, bool value) 
    { 
      obj.SetValue(IsMonitoringProperty, value); 
    } 

    public static readonly DependencyProperty IsMonitoringProperty = 
        DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(PasswordBoxMonitor), new UIPropertyMetadata(false, OnIsMonitoringChanged)); 



    public static int GetPasswordLength(DependencyObject obj) 
    { 
      return (int)obj.GetValue(PasswordLengthProperty); 
    } 

    public static void SetPasswordLength(DependencyObject obj, int value) 
    { 
      obj.SetValue(PasswordLengthProperty, value); 
    } 

    public static readonly DependencyProperty PasswordLengthProperty = 
        DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordBoxMonitor), new UIPropertyMetadata(0)); 

    private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
      var pb = d as PasswordBox; 
      if (pb == null) 
      { 
        return; 
      } 
      if ((bool) e.NewValue) 
      { 
        pb.PasswordChanged += PasswordChanged; 
      } 
      else 
      { 
        pb.PasswordChanged -= PasswordChanged; 
      } 
    } 

    static void PasswordChanged(object sender, RoutedEventArgs e) 
    { 
      var pb = sender as PasswordBox; 
      if (pb == null) 
      { 
        return; 
      } 
      SetPasswordLength(pb, pb.Password.Length); 
    } 
  } 
} 

参考:水印文本框的 WPF 水印密码框 https://stackoverflow.com/questions/1607066/wpf-watermark-passwordbox-from-watermark-textbox/1610761#1610761

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 wpf 密码框设置一些默认文本? [复制] 的相关文章

随机推荐

  • 将 cmake 与自定义文件生成器结合使用

    我想使用 CMake 生成混淆的 lua 文件以供交付 在我的一生中 我无法让 add custom command add custom target 为我构建这些文件 我缺少一些东西 ADD CUSTOM TARGET LUABIND
  • 使用 DropWizard Hibernate 进行单元测试时显示“当前没有会话绑定到执行上下文”

    我将 DW 与 Hibernate 结合使用 并尝试为我的 DAO 编写单元测试 我的代码基于这个例子 https github com dropwizard dropwizard blob 1310d7c981581b4e1be97104
  • 是否可以更改 WPF 控件的父级

    是否可以更改 WPF 控件的父控件 这是一个例子 StackPanel堆栈1有Buttonbtn1 在里面 还有一个空的StackPanel堆栈2 我想以编程方式将 btn1 移动到 stack2 谢谢您的帮助 您可以通过以下方式执行此操作
  • 将尾部输出重定向到程序中

    我想使用 tail 作为标准输入向程序发送文本文件中的最新行 首先 我向程序回显一些每次都相同的输入 然后从输入文件发送尾部输入 该输入文件应首先通过 sed 处理 以下是我期望工作的命令行 但是当程序运行时 它只接收回显输入 而不接收尾部
  • 混淆 bash 中存储的密码

    我有一个 bash 脚本 我需要编写密码才能运行程序 其他人可以看到它 有没有办法把密码写得不太明显 即使他可以在 bash 中执行相同的命令并获取密码 他也无法读取文本形式的密码 今天我这样做 PASSWORD 1234567 progr
  • TypeScript 中类似字典的对象应使用什么类型?

    在 TypeScript 中 我有时会使用期望 或返回 对象的函数 其中这些对象被视为字典 基本上我的问题是 这里使用的正确类型是什么 我可以用 Record
  • 使用 asyncore 读取网站

    我想异步阅读一个网站 据我所知 这是不可能的 urllib 现在我尝试使用普通套接字进行阅读 但是 HTTP 给我带来了麻烦 我遇到了各种时髦的编码 例如传输编码 分块 必须手动解析所有这些东西 我现在想编码 C 而不是 python 难道
  • C++ 析构函数和函数调用顺序

    假设我有以下代码片段 Foo foo return bar 现在 C 标准是否保证 bar 将在 foo Foo 之前调用 或者这是编译器 实现的选择 Thanks 这是有保证的行为 实际执行过程如下 0 enter block scope
  • C++ 令人头疼的命名空间

    好吧 这个问题已经发生了一些变化 我想尝试从我正在追求的基本目标开始 重新 创建在 C 资源获取和初始化中包装遗留 C 语言实体的库代码 并提供基本或更好的异常保证 使此代码的客户端能够以非常自然的 C 方式使用它 而不会为现有代码创建大量
  • JS 是否支持使用键函数而不是比较器进行排序?

    JavaScript 的array sort https developer mozilla org en US docs Web JavaScript Reference Global Objects Array sort Syntax方
  • 在 R 闪亮应用程序中评级星星

    我正在尝试向我闪亮的应用程序添加一些元素 以使其看起来更好 因此 我正在使用新的shiny semantic包允许以简单的方式添加语义 UI 元素 人们可以在这里找到闪亮的语义元素的示例 http demo appsilondatascie
  • 从服务(IntentService)和活动执行的AsyncTask - 有区别吗?

    从 Activity 或 IntentService 启动 AsyncSync 之间有什么区别吗 我正在构建一个通过 http 下载和上传文件的应用程序 我为每次传输使用带有进度条的自定义通知布局 我选择并行传输或将它们放入队列 您会推荐哪
  • AWS CloudFront 重定向到 S3 存储桶

    我创建了一个 CloudFront 发行版来为静态网站提供服务 S3是源服务器 现在 如果我们访问 CloudFront URL 它会重定向到 S3 位置 d2s18t7gwlicql cloudfront net or test tele
  • 计算 QR 码对齐图案的位置

    我需要知道如何计算 QR 码对齐图案的位置 如中定义ISO IEC 18004 2000 附录 E 表 http www arscreatio com repositorio images n 23 SC031 N 1915 18004Te
  • Chrome DevTools 脚本黑盒不起作用

    我正在尝试使用 chrome devtools 的新功能 黑盒脚本 这篇 Chrome Devtools 文章列出了脚本黑盒功能 https developer chrome com devtools docs blackboxing wh
  • C++ win32设置光标位置

    我知道要使用哪个功能 但我无法让它正常工作 我用了SetCursorPos 唯一的问题是它将光标设置为屏幕坐标而不是窗口坐标 我也尝试过ScreenToClient 但它并没有起作用 这是我的代码 pt x 113 pt y 280 Scr
  • 本地通知替代周重复

    我有两个通知 我想隔周重复一次 例如 在第一周的星期一设置一个通知 并应在第三周的星期一重复 第二次通知设置在第二周的星期二 并且应在第四周的星期二重复 为此我该怎么办 获取周数 并根据周数做出决定
  • 包含一个树枝文件并从单独的文件传递变量?

    我有container twig 包括component twig并传递一个名为 mock 的对象 在container twig中 set mock title This is my title include component twi
  • 解释一下 $CI =& get_instance();

    查看codeigniter的源代码 在它的辅助函数中我不断看到代码 CI get instance 谁能向我解释一下这段代码是如何工作的 我知道它正在返回对 CI 超级对象的引用 但是在哪里get instance 来自 这基本上是一个单例
  • 如何在 wpf 密码框设置一些默认文本? [复制]

    这个问题在这里已经有答案了 可能的重复 WPF 中的水印文本框 https stackoverflow com questions 833943 watermark textbox in wpf 我可以知道如何在 WPF 的密码框中放入一些