• WPF的TextBox以及PasswordBox显示水印文字


    1、TextBox

    <ControlTemplate x:Key="WaterMarkTextBox" TargetType="{x:Type TextBox}">
                                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                                            BorderThickness="{TemplateBinding BorderThickness}" 
                                            Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                        <Grid>
                                            <ScrollViewer x:Name="PART_ContentHost" Focusable="False" 
                                                          HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    
                                            <TextBlock x:Name="InternalWatermarkLabel" 
                                                       Text="{TemplateBinding Tag}"
                                                       FontStyle="Italic"
                                                       Visibility="Collapsed" Focusable="False"
                                                       Foreground="Silver"/>
                                        </Grid>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <MultiTrigger>
                                            <MultiTrigger.Conditions>
                                                <Condition Property="IsFocused" Value="False" />
                                                <Condition Property="Text" Value="" />
                                            </MultiTrigger.Conditions>
                                            <MultiTrigger.Setters>
                                                <Setter Property="Visibility" TargetName="InternalWatermarkLabel" Value="Visible" />
                                            </MultiTrigger.Setters>
                                        </MultiTrigger>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                                        </Trigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                                        </Trigger>
                                        <Trigger Property="IsKeyboardFocused" Value="True">
                                            <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
         <TextBox Grid.Row="0" Grid.Column="1" Margin="0,15" Text="abcdefg" x:Name="tb_PassPhrase" VerticalAlignment="Center"
                                     Tag="Pass Phrase" Template="{DynamicResource WaterMarkTextBox}">
                            </TextBox>

    2、PasswordBox

            <Style x:Key="{x:Type PasswordBox}" TargetType="{x:Type PasswordBox}">
                <Setter Property="WpfStyle:PasswordBoxMonitor.IsMonitoring" Value="True"/>
                <Setter Property="Margin" Value="0,15"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type PasswordBox}">
                            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                                            BorderThickness="{TemplateBinding BorderThickness}" 
                                            Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <Grid>
                                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" 
                                                          HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    
                                    <TextBlock x:Name="InternalWatermarkLabel" 
                                                       Text="{TemplateBinding Tag}"
                                                       FontStyle="Italic"
                                                       Visibility="Collapsed" Focusable="False"
                                                       Foreground="Silver"/>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsFocused" Value="False" />
                                        <Condition Property="WpfStyle:PasswordBoxMonitor.PasswordLength" Value="0" />
                                    </MultiTrigger.Conditions>
                                    <MultiTrigger.Setters>
                                        <Setter Property="Visibility" TargetName="InternalWatermarkLabel" Value="Visible" />
                                    </MultiTrigger.Setters>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                                </Trigger>
                                <Trigger Property="IsKeyboardFocused" Value="True">
                                    <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    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 readonly DependencyProperty PasswordLengthProperty =
              DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordBoxMonitor), new UIPropertyMetadata(0));
    
            public static int GetPasswordLength(DependencyObject obj)
            {
                int length = (int)obj.GetValue(PasswordLengthProperty);
                System.Diagnostics.Debug.WriteLine("Password Length:{0}!!!!!!!!!!!!!!", length.ToString());
                return length;
            }
    
            public static void SetPasswordLength(DependencyObject obj, int value)
            {
                obj.SetValue(PasswordLengthProperty, value);
            }      
    
            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);
            }
        }
  • 相关阅读:
    windows 系列机器查看tcp 配置
    test windows live writer
    win7 mysql 启动 问题
    用enum枚举量做下标初始化数组
    二层VXLAN静态配置
    pip install scrapy报错原文
    python练习2
    pycharm字体大小设置快捷键的方法
    python练习1
    centos 7下修改IP地址为静态
  • 原文地址:https://www.cnblogs.com/AlvinLiang/p/5221240.html
Copyright © 2020-2023  润新知