bobmarley12345

wpf password box hint/placeholder text

Feb 18th, 2022 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 KB | None | 0 0
  1. <Style x:Key="HintedPasswordBox" TargetType="{x:Type PasswordBox}">
  2.     <Setter Property="PasswordChar" Value="●" />
  3.     <Setter Property="Background" Value="{DynamicResource TextBox.Static.Background}" />
  4.     <Setter Property="BorderBrush" Value="{DynamicResource TextBox.Static.Border}" />
  5.     <Setter Property="Foreground" Value="{DynamicResource AREghZyBrush.Foreground.Static}" />
  6.     <Setter Property="BorderThickness" Value="1" />
  7.     <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
  8.     <Setter Property="HorizontalContentAlignment" Value="Left" />
  9.     <Setter Property="FocusVisualStyle" Value="{x:Null}" />
  10.     <Setter Property="AllowDrop" Value="true" />
  11.     <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />
  12.     <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
  13.     <Setter Property="ap:PasswordAttachedProperty.ListenToLength" Value="True"/>
  14.     <Setter Property="Template">
  15.         <Setter.Value>
  16.             <ControlTemplate TargetType="{x:Type PasswordBox}">
  17.                 <Grid>
  18.                     <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
  19.                         <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
  20.                     </Border>
  21.                     <TextBlock IsHitTestVisible="False"
  22.                                Text="{TemplateBinding Tag}"
  23.                                x:Name="PART_Placeholder"
  24.                                FontFamily="{TemplateBinding FontFamily}"
  25.                                FontSize="{TemplateBinding FontSize}"
  26.                                Margin="3 0 0 0"
  27.                                Padding="{TemplateBinding Padding}"
  28.                                VerticalAlignment="Center"
  29.                                HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
  30.                                Foreground="{DynamicResource AREghZyBrush.Foreground.Disabled}">
  31.                         <TextBlock.Style>
  32.                             <Style TargetType="{x:Type TextBlock}">
  33.                                 <Setter Property="Visibility" Value="Collapsed"/>
  34.                                 <Style.Triggers>
  35.                                     <DataTrigger Binding="{Binding (ap:PasswordAttachedProperty.InputLength), RelativeSource={RelativeSource TemplatedParent}}" Value="0">
  36.                                         <Setter Property="Visibility" Value="Visible"/>
  37.                                     </DataTrigger>
  38.                                     <MultiDataTrigger>
  39.                                         <MultiDataTrigger.Conditions>
  40.                                             <Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
  41.                                             <Condition Binding="{Binding (ap:TextHinting.ShowWhenFocused), RelativeSource={RelativeSource TemplatedParent}}" Value="False"/>
  42.                                         </MultiDataTrigger.Conditions>
  43.                                         <MultiDataTrigger.Setters>
  44.                                             <Setter Property="Visibility" Value="Collapsed"/>
  45.                                         </MultiDataTrigger.Setters>
  46.                                     </MultiDataTrigger>
  47.                                 </Style.Triggers>
  48.                             </Style>
  49.                         </TextBlock.Style>
  50.                     </TextBlock>
  51.                 </Grid>
  52.                 <ControlTemplate.Triggers>
  53.                     <Trigger Property="IsEnabled" Value="false">
  54.                         <Setter Property="Opacity" TargetName="border" Value="0.56" />
  55.                     </Trigger>
  56.                     <Trigger Property="IsMouseOver" Value="true">
  57.                         <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource TextBox.MouseOver.Border}" />
  58.                     </Trigger>
  59.                     <Trigger Property="IsKeyboardFocused" Value="true">
  60.                         <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource TextBox.Focus.Border}" />
  61.                     </Trigger>
  62.                 </ControlTemplate.Triggers>
  63.             </ControlTemplate>
  64.         </Setter.Value>
  65.     </Setter>
  66.     <Style.Triggers>
  67.         <MultiTrigger>
  68.             <MultiTrigger.Conditions>
  69.                 <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true" />
  70.                 <Condition Property="IsSelectionActive" Value="false" />
  71.             </MultiTrigger.Conditions>
  72.             <Setter Property="SelectionBrush" Value="{DynamicResource TextBox.Selection.Inactive}" />
  73.         </MultiTrigger>
  74.     </Style.Triggers>
  75. </Style>
  76.  
  77. public class PasswordAttachedProperty {
  78.     public static readonly DependencyProperty ListenToLengthProperty =
  79.         DependencyProperty.RegisterAttached(
  80.             "ListenToLength",
  81.             typeof(bool),
  82.             typeof(PasswordAttachedProperty),
  83.             new FrameworkPropertyMetadata(false, PropertyChangedCallback));
  84.  
  85.     private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  86.         if (e.NewValue == e.OldValue) {
  87.             return;
  88.         }
  89.  
  90.         if (d is PasswordBox box) {
  91.             if ((bool) e.NewValue) {
  92.                 // just in case...
  93.                 box.PasswordChanged -= BoxOnPasswordChanged;
  94.                 box.PasswordChanged += BoxOnPasswordChanged;
  95.             }
  96.             else {
  97.                 box.PasswordChanged -= BoxOnPasswordChanged;
  98.             }
  99.         }
  100.         else {
  101.             throw new Exception("DependencyObject is not a password box. It is '" + (d == null ? "null" : d.GetType().Name) + '\'');
  102.         }
  103.     }
  104.  
  105.     public static readonly DependencyProperty InputLengthProperty =
  106.         DependencyProperty.RegisterAttached(
  107.             "InputLength",
  108.             typeof(int),
  109.             typeof(PasswordAttachedProperty),
  110.             new FrameworkPropertyMetadata(0));
  111.  
  112.     public static bool GetListenToLength(PasswordBox box) {
  113.         return (bool) box.GetValue(ListenToLengthProperty);
  114.     }
  115.  
  116.     public static void SetListenToLength(PasswordBox box, bool value) {
  117.         box.SetValue(ListenToLengthProperty, value);
  118.     }
  119.  
  120.     public static int GetInputLength(PasswordBox box) {
  121.         return (int) box.GetValue(InputLengthProperty);
  122.     }
  123.  
  124.     public static void SetInputLength(PasswordBox box, int value) {
  125.         box.SetValue(InputLengthProperty, value);
  126.     }
  127.  
  128.     private static void BoxOnPasswordChanged(object sender, RoutedEventArgs e) {
  129.         SetInputLength((PasswordBox) sender, ((PasswordBox) sender).SecurePassword.Length);
  130.     }
  131. }
  132.  
  133. public static class TextHinting {
  134.     public static readonly DependencyProperty ShowWhenFocusedProperty =
  135.         DependencyProperty.RegisterAttached(
  136.             "ShowWhenFocused",
  137.             typeof(bool),
  138.             typeof(TextHinting),
  139.             new FrameworkPropertyMetadata(false));
  140.  
  141.     public static void SetShowWhenFocused(Control control, bool value) {
  142.         if (control is TextBoxBase || control is PasswordBox) {
  143.             control.SetValue(ShowWhenFocusedProperty, value);
  144.         }
  145.  
  146.         throw new ArgumentException("Control was not a textbox", "control");
  147.     }
  148.  
  149.     public static bool GetShowWhenFocused(Control control) {
  150.         if (control is TextBoxBase || control is PasswordBox) {
  151.             return (bool) control.GetValue(ShowWhenFocusedProperty);
  152.         }
  153.  
  154.         throw new ArgumentException("Control was not a textbox", "control");
  155.     }
  156. }
  157.  
  158. https://github.com/AngryCarrot789/REghZyAccountManagerV6
Add Comment
Please, Sign In to add comment