本文介绍了在wpf中设置密码框的水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共类PasswordBoxHelper
    {
      静态布尔值isInistialized = false;

      公共静态字符串GetWatermark(DependencyObject obj)
       {
           return(string)obj.GetValue(WatermarkProperty);
       }

      公共静态无效值SetWatermark(DependencyObject obj,字符串值)
       {
           obj.SetValue(WatermarkProperty,value);
       }

      公共静态只读DependencyProperty WatermarkProperty =
           DependencyProperty.RegisterAttached("Watermark",typeof(string),typeof(PasswordBoxHelper),新的UIPropertyMetadata(null,WatermarkChanged));



      公共静态布尔GetShowWatermark(DependencyObject obj)
       {
           return(bool)obj.GetValue(ShowWatermarkProperty);
       }

      公共静态无效值SetShowWatermark(DependencyObject obj,bool值)
       {
           obj.SetValue(ShowWatermarkProperty,value);
       }

      公共静态只读DependencyProperty ShowWatermarkProperty =
           DependencyProperty.RegisterAttached("ShowWatermark",typeof(bool),typeof(PasswordBoxHelper),new UIPropertyMetadata(false));



      静态无效WatermarkChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e)
       {
           var pwd = obj as PasswordBox;

           CheckShowWatermark(pwd);

          如果(!isInistialized)
           {
                             pwd.PasswordChanged + =新的RoutedEventHandler(pwd_PasswordChanged);
                             pwd.Unloaded + =新的RoutedEventHandler(pwd_Unloaded);
                             isInistialized = true;
           }
       }

      私有静态无效CheckShowWatermark(PasswordBox pwd)
       {
           pwd.SetValue(PasswordBoxHelper.ShowWatermarkProperty,pwd.Password == string.Empty);
       }

      静态无效pwd_PasswordChanged(对象发送者,RoutedEventArgs e)
       {
           var pwd =发件人为PasswordBox;
           CheckShowWatermark(pwd);
       }

      静态无效pwd_Unloaded(对象发送者,RoutedEventArgs e)
       {
           var pwd =发件人为PasswordBox;
          如果(pwd.Focus()== true)
           {
                             CheckShowWatermark(pwd);
           }
           pwd.PasswordChanged-=新的RoutedEventHandler(pwd_PasswordChanged);
       }

    }

public class PasswordBoxHelper
    {
        static bool isInistialised = false;

        public static string GetWatermark(DependencyObject obj)
        {
            return (string)obj.GetValue(WatermarkProperty);
        }

        public static void SetWatermark(DependencyObject obj, string value)
        {
            obj.SetValue(WatermarkProperty, value);
        }

        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(PasswordBoxHelper), new UIPropertyMetadata(null, WatermarkChanged));



        public static bool GetShowWatermark(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowWatermarkProperty);
        }

        public static void SetShowWatermark(DependencyObject obj, bool value)
        {
            obj.SetValue(ShowWatermarkProperty, value);
        }

        public static readonly DependencyProperty ShowWatermarkProperty =
            DependencyProperty.RegisterAttached("ShowWatermark", typeof(bool), typeof(PasswordBoxHelper), new UIPropertyMetadata(false));



        static void WatermarkChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var pwd = obj as PasswordBox;

            CheckShowWatermark(pwd);

            if (!isInistialised)
            {
                pwd.PasswordChanged += new RoutedEventHandler(pwd_PasswordChanged);
                pwd.Unloaded += new RoutedEventHandler(pwd_Unloaded);
                isInistialised = true;
            }
        }

        private static void CheckShowWatermark(PasswordBox pwd)
        {
            pwd.SetValue(PasswordBoxHelper.ShowWatermarkProperty, pwd.Password == string.Empty);
        }

        static void pwd_PasswordChanged(object sender, RoutedEventArgs e)
        {
            var pwd = sender as PasswordBox;
            CheckShowWatermark(pwd);
        }

        static void pwd_Unloaded(object sender, RoutedEventArgs e)
        {
            var pwd = sender as PasswordBox;
            if (pwd.Focus() == true)
            {
                CheckShowWatermark(pwd);
            }
            pwd.PasswordChanged -= new RoutedEventHandler(pwd_PasswordChanged);
        }

    }

< PasswordBox Margin =" 10" x:Name ="pwd";宽度="100". Template ="{StaticResource WatermarkedPasswordBoxTemplate}" local:PasswordBoxHelper.Watermark ="密码" Grid.Row ="1". Grid.Column ="1". Horizo​​ntalAlignment =中心". VerticalAlignment =中心". />

<PasswordBox Margin="10" x:Name="pwd" Width="100" Template="{StaticResource WatermarkedPasswordBoxTemplate}" local:PasswordBoxHelper.Watermark="Password" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />

在这里我要设置水印控件...

here i want to set watermark control...

当密码箱具有焦点等于true时,请删除水印

when passwordbox has focus is equal to true then remove the watermark

否则它等于false然后得到水印

otherwise it is equal to false then get the watermark

推荐答案

您可以这样做吗?

https://social.msdn.microsoft.com/Forums/zh-CN/d7e3c5cd-7fd9-4f4a-b858-1337cebaae67/validation-error-problem?forum=wpf

https://social.msdn.microsoft.com/Forums/zh-CN/f684b33f-f22f-453d-aea8-8ec766740eb6/use-multiple-class-in-wpf?forum=wpf

https://social.msdn.microsoft.com/Forums/zh-CN/167cd5b4-ab50-4f52-9056-dc57cdc33218/nullreferanceexception-was-unhandled-by-usercode?forum=wpf

谢谢.


这篇关于在wpf中设置密码框的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:22