如何在passwordBox wpf上设置一些默认文本? [英] how to set some default text at the passwordBox wpf?

查看:144
本文介绍了如何在passwordBox wpf上设置一些默认文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
WPF中的水印文本框

Possible Duplicate:
Watermark TextBox in WPF

我可以知道如何在WPF的密码框中放入一些默认文本吗? 我以MSN Messenger为例.默认情况下,密码字段将在密码框中显示密码".当我单击并在密码框中键入内容时,密码"文本将消失,圆点将代替它.

May i know how to put some default texts in the password box in WPF? I use MSN messenger as an example. When its default, the password field will show the "password" in the Password Box. When i click and type on the password box, the "password" text will disappear and the dots will replace it.

推荐答案

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

You have to create a custom control to do that.

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#代码:

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

参考: Watermark TextBox中的WPF Watermark PasswordBox

这篇关于如何在passwordBox wpf上设置一些默认文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆