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);
        }
    }

 

posted @ 2016-02-26 18:06  AlvinLiang  阅读(2183)  评论(0编辑  收藏  举报