分享

如何在WPF PasswordBox中设置一些默认的文本?

 牛人的尾巴 2016-03-22
  

如何在WPF PasswordBox中设置一些默认的文本?



c# wpf
可能重复: 水印文本框在WPF 可我知道如何把默认文本中的密码箱在WPF? 作为一个例子。当它默认情况下,密码字段将在密码框中显示“密码”。当我点击和输入的密码箱,在“密码”文本将消失,这些片断会取代它。
本文地址 :CodeGo.net/1075823/
-------------------------------------------------------------------------------------------------------------------------
1.你必须创建一个自定义的控件来做到这一点。 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); 
 } 
 } 
} 

参考:WPF水印的PasswordBox从水印文本框
本文标题 :如何在WPF PasswordBox中设置一些默认的文本?
本文地址 :CodeGo.net/1075823/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多