2

私は wpf アプリケーションを持っており、制御できない理由で mvvm パターンに注意深く従っています。自分では制御できないセキュリティ上の理由から、PasswordBox にデータバインドしたくありません。ログインに失敗したときにパスワード ボックスの内容をクリアするにはどうすればよいですか? 私はxamlでそうする方法を好むでしょう。

4

1 に答える 1

4

添付 ファイルを作成DependencyPropertyして、XAML またはコードで使用できます。例:

のリスト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:

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

Trigger(で)で使用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>

Code behind

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();
            }
        }
    }
}
于 2013-07-19T16:25:47.687 に答える