01_WPF+Prism登入之PasswordBox的Binding

野码發表於2024-05-18
#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>

相關文章