zoukankan      html  css  js  c++  java
  • 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);
            }
        }
  • 相关阅读:
    理解Promise函数中的resolve和reject
    一行代码 去除滚动条
    django开发前准备工作
    居中select中的option选项
    setTimeout中第一个参数
    利用es6解构赋值快速提取JSON数据;
    需要删除远程分支,刚好有正在别的分支做的事情
    vue项目处理dpr和多屏幕适配问题
    使用mockjs模拟后端返回的json数据;
    移动端页面中点击input输入框的时候弹出的键盘将输入框挡住的问题
  • 原文地址:https://www.cnblogs.com/AlvinLiang/p/5221240.html
Copyright © 2011-2022 走看看