小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

如何在WPF PasswordBox中設(shè)置一些默認(rèn)的文本?

 牛人的尾巴 2016-03-22
  

如何在WPF PasswordBox中設(shè)置一些默認(rèn)的文本?



c# wpf
可能重復(fù): 水印文本框在WPF 可我知道如何把默認(rèn)文本中的密碼箱在WPF? 作為一個(gè)例子。當(dāng)它默認(rèn)情況下,密碼字段將在密碼框中顯示“密碼”。當(dāng)我點(diǎn)擊和輸入的密碼箱,在“密碼”文本將消失,這些片斷會(huì)取代它。
本文地址 :CodeGo.net/1075823/
-------------------------------------------------------------------------------------------------------------------------
1.你必須創(chuàng)建一個(gè)自定義的控件來做到這一點(diǎn)。 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從水印文本框
本文標(biāo)題 :如何在WPF PasswordBox中設(shè)置一些默認(rèn)的文本?
本文地址 :CodeGo.net/1075823/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多