首页 > 其他分享 >01_WPF+Prism登录之PasswordBox的Binding

01_WPF+Prism登录之PasswordBox的Binding

时间:2024-05-18 12:52:12浏览次数:14  
标签:01 string PasswordBox Binding Pwd passwordBox Password public

#region 登录信息/// <summary>
        /// 密码
        /// </summary>
        private string _Pwd;

        /// <summary>
        /// 密码
        /// </summary>
        public string Pwd
        {
            get { return _Pwd; }
            set
            {
                _Pwd = value;
                RaisePropertyChanged();
            }
        }
        #endregion

 

 /// <summary>
    /// PasswordBox扩展属性
    /// </summary>
    public class PasswordBoxExtend
    {
        public static string GetPwd(DependencyObject obj)
        {
            return (string)obj.GetValue(PwdProperty);
        }

        public static void SetPwd(DependencyObject obj, string value)
        {
            obj.SetValue(PwdProperty, value);
        }

        // Using a DependencyProperty as the backing store for Pwd.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PwdProperty =
            DependencyProperty.RegisterAttached("Pwd", typeof(string), typeof(PasswordBoxExtend), new PropertyMetadata("", OnPwdChanged));

        /// <summary>
        /// 自定附加属性发生变化 改变Password属性
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnPwdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox pwdBox = d as PasswordBox;
            string newPwd = (string)e.NewValue;//新的值
            if (pwdBox != null && pwdBox.Password != newPwd)
            {
                pwdBox.Password = newPwd;
            }
        }
    }

    /// <summary>
    /// Password行为 Password变化,自定义附加属性跟着变化
    /// </summary>
    public class PasswordBoxBehavior : Behavior<PasswordBox>
    {
        /// <summary>
        /// 附加 注入事件
        /// </summary>
        protected override void OnAttached()
        {

            base.OnAttached();
            AssociatedObject.PasswordChanged += OnPasswordChanged;
        }

        /// <summary>
        /// Password变化,自定义附加属性跟着变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            string password = PasswordBoxExtend.GetPwd(passwordBox);//附加属性值

            if (passwordBox != null && passwordBox.Password != password)
            {
                PasswordBoxExtend.SetPwd(passwordBox, passwordBox.Password);
            }
        }

        /// <summary>
        /// 销毁 移出事件
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.PasswordChanged -= OnPasswordChanged;
        }
    }

 

xmlns:pwdEx="clr-namespace:LogInModule.Extensions"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"



<StackPanel Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Right"> <TextBlock Text="密码:" /> </StackPanel> <StackPanel Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"> <PasswordBox Style="{StaticResource pswbox}" /> </StackPanel>

 

  <Style x:Key="pswbox" TargetType="PasswordBox">
            <Setter Property="Height" Value="30"/>
            <Setter Property="Width" Value="300"/>
            <Setter Property="Background" Value="#7FFF00"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="PasswordBox">
                        <Border Background="{TemplateBinding Background}" CornerRadius="15" BorderThickness="1" BorderBrush="#C0FF3E" >
                            <StackPanel>
                                <Grid>
                                    <PasswordBox Margin="0,7" Background="#7FFF00"
                                             BorderBrush="Transparent"
                                             pwdEx:PasswordBoxExtend.Pwd="{Binding Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                             BorderThickness="0" >
                                        <i:Interaction.Behaviors>
                                            <pwdEx:PasswordBoxBehavior></pwdEx:PasswordBoxBehavior>
                                        </i:Interaction.Behaviors>
                                    </PasswordBox>
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Grid>
                            </StackPanel>

                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 

标签:01,string,PasswordBox,Binding,Pwd,passwordBox,Password,public
From: https://www.cnblogs.com/MingQiu/p/18199231

相关文章

  • 力扣-901. 股票价格跨度
    1.题目题目地址(901.股票价格跨度-力扣(LeetCode))https://leetcode.cn/problems/online-stock-span/题目描述设计一个算法收集某些股票的每日报价,并返回该股票当日价格的跨度。当日股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包......
  • P10125 「Daily OI Round 3」Simple 题解
    题目传送门简单模拟,主要考察字符串。首先输入一个char类型的数组,然后直接遍历每一位是否为Acoipp或Svpoll即可。//Simple//codeby:cq_irritater//time:2024/02/04#include<bits/stdc++.h>usingnamespacestd;chara[10];intmain(){//freopen("......
  • P8684 [蓝桥杯 2019 省 B] 灵能传输 题解
    题目传送门本题涉及到了\(3\)种算法:前缀和,排序以及贪心。(1)前缀和本题实际上要求通过某种灵能传输可以使得该序列的最大值最小。而由前缀和可知,当某一个前缀和序列保持有序(或前缀和序列表示的函数单调)时,其\(s[i]-s[i-1]\)的最大值可以达到最小。通过对几个样例的观察......
  • P8679 [蓝桥杯 2019 省 B] 填空问题 题解
    题目传送门试题A:组队【解析】本题是一道经典的DFS搜索题,每次对各号位的选手进行DFS,找到各号位上有成绩的选手时再进行进一步搜索即可。【程序】#include<bits/stdc++.h>usingnamespacestd;intteam[20][6];intmax_sum;boolvis[20];voiddfs(intu,intsu......
  • Day01
    目录一、前期准备(一)cmd1、打开cmd2、常见cmd命令(二)环境变量(三)JDK(四)配置环境变量(五)Java的加载与执行(六)java三大使用平台(七)java的主要特性(八)高级语言的编译运行方式(九)JRE和JDK二、java基础语法(一)注释(二)关键字(三)字面量(五)计算机的存储规则(六)数据类型一、前期准备计算机包括两部分......
  • Games101-3 triangle
    rasterize==drawingontothescreencolor=(red,green,blue)pixelindicesarefrom(0,0)to(width-1,height-1)pixel(x,y)iscenteredat(x+0.5,y+0.5)光栅化判断一个像素的中心点是否需要draw采样的方法--将函数离散化如果中心再三角形内。如何判断......
  • Games101-5 shadering
    shader对不同物体应用不同的材质定义:shading!=shadowdiffusereflection漫反射光照角度不同,则反射程度也不同于此同时物体离光源越远,反射程度越低高光项镜面反射和视线比较接近的时候使用半程向量计算高光注:半程向量比较好算,反射向量比较难算指数p:cos......
  • Games101-6 Geometry
    implicit--隐式几何explicit--显示几何implicit点不需要知道位置,但是可以用点之间的关系表示(按照类别归类)E.g.allpointsin3D,where$x2+y2+z^2=1$更通用的表示$f(x,y,z)=0$劣势:不直观优势:可以很简单的判断一个点是否再物体内或者外。explicit......
  • Games101-7 raytracing
    shadowmapping思想:光源可以看到点,人也可以看到的点。---不在shadow中的点只能处理点光源深度不一致浮点数的精度问题。软/硬阴影raytracing直线传播不会碰撞从光源出发,到人眼光线是可以反射的多次弹射的光纤追踪rayequation对隐式表面对显示......
  • Games101-7 raytracing2
    辐射度量学basicradiometry---精确的描述光光线的强度Iis10。在屋里层次准确的描述光Newterms:radiantfluxintensityirradianceradianceradiantenergyandflux,radiantintensityRadiantintensity中角度是如何定义的单位立体角Radiantinte......