如何在没有数据绑定的情况下登录失败时清除密码框的内容? [英] How to clear the contents of a PasswordBox when login fails without databinding?

查看:46
本文介绍了如何在没有数据绑定的情况下登录失败时清除密码框的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wpf 应用程序,并且出于我无法控制的原因,我正在仔细遵循 mvvm 模式.出于我无法控制的安全原因,我不想将数据绑定到我的 PasswordBox.登录失败时如何清除密码框中的内容?我更喜欢在 xaml 中这样做.

I have a wpf application and I am following the mvvm pattern carefully for reasons beyond my control. I do not want to databind to my PasswordBox for security reasons beyond my control. How do I clear the contents of the password box when the login fails? I would prefer a way to do so in xaml.

推荐答案

您可以创建附加 DependencyProperty 并将其用作 XAML 或在代码中使用.示例:

You can create your attached DependencyProperty and use it as a XAML or in code. Example:

PasswordBehaviors 列表:

public static class PasswordBehaviors
{
    public static void SetIsClear(DependencyObject target, bool value)
    {
        target.SetValue(IsClearProperty, value);
    }

    public static readonly DependencyProperty IsClearProperty =
                                              DependencyProperty.RegisterAttached("IsClear",
                                              typeof(bool),
                                              typeof(PasswordBehaviors),
                                              new UIPropertyMetadata(false, OnIsClear));

    private static void OnIsClear(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is bool && ((bool)e.NewValue) == true)
        {
            PasswordBox MyPasswordBox = sender as PasswordBox;

            if (MyPasswordBox != null)
            {
                MyPasswordBox.Clear();
            }
        }
    }
}

EventTrigger 一起使用:

<EventTrigger SourceName="Clear" RoutedEvent="Button.Click">
    <BeginStoryboard>
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyPasswordBox" Storyboard.TargetProperty="(local:PasswordBehaviors.IsClear)">
                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                    <DiscreteObjectKeyFrame.Value>
                        <sys:Boolean>True</sys:Boolean>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>

DataTrigger 一起使用(在 Style/DataTemplate/etc 中):

Using with DataTrigger (in Style/DataTemplate/etc):

<DataTrigger Binding="{Binding ElementName=LoginElementFailed, Path=Status), Mode=OneWay}" Value="True">
    <Setter Property="(local:PasswordBehaviors.IsClear)" Value="True" />
</DataTrigger>

Trigger 一起使用(在 Style 中):

Using with Trigger (in Style):

<Trigger Property="LoginFailed.IsChecked" Value="True">
    <Setter Property="(local:PasswordBehaviors.IsClear)" Value="True" />
</Trigger>

使用后台代码:

private void Clear_Click(object sender, RoutedEventArgs e)
{
    PasswordBehaviors.SetIsClear(MyPasswordBox, true);
}

完整示例:

XAML

<Window x:Class="ClearPasswordBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ClearPasswordBox"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen">

<Grid>
    <Grid.Triggers>
        <EventTrigger SourceName="Clear" RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyPasswordBox" Storyboard.TargetProperty="(local:PasswordBehaviors.IsClear)">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                            <DiscreteObjectKeyFrame.Value>
                                <sys:Boolean>True</sys:Boolean>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger SourceName="ResetClear" RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyPasswordBox" Storyboard.TargetProperty="(local:PasswordBehaviors.IsClear)">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                            <DiscreteObjectKeyFrame.Value>
                                <sys:Boolean>False</sys:Boolean>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>

    <PasswordBox Name="MyPasswordBox" local:PasswordBehaviors.IsClear="False" Width="100" Height="30" />

    <Button Name="Clear" Width="100" Height="30" HorizontalAlignment="Right" Content="Clear" />

    <Button Name="ResetClear" Width="100" Height="30" HorizontalAlignment="Left" Content="ResetClear" />
</Grid>
</Window>

背后的代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    //private void Clear_Click(object sender, RoutedEventArgs e)
    //{
    //    PasswordBehaviors.SetIsClear(MyPasswordBox, true);
    //}

    //private void ResetClear_Click(object sender, RoutedEventArgs e)
    //{
    //    PasswordBehaviors.SetIsClear(MyPasswordBox, false);
    //}
}

public static class PasswordBehaviors
{
    public static void SetIsClear(DependencyObject target, bool value)
    {
        target.SetValue(IsClearProperty, value);
    }

    public static readonly DependencyProperty IsClearProperty =
                                              DependencyProperty.RegisterAttached("IsClear",
                                              typeof(bool),
                                              typeof(PasswordBehaviors),
                                              new UIPropertyMetadata(false, OnIsClear));

    private static void OnIsClear(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is bool && ((bool)e.NewValue) == true)
        {
            PasswordBox MyPasswordBox = sender as PasswordBox;

            if (MyPasswordBox != null)
            {
                MyPasswordBox.Clear();
            }
        }
    }
}

这篇关于如何在没有数据绑定的情况下登录失败时清除密码框的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆