如果使用 Xaml 的方法失败,则反向触发动画,而不是代码隐藏

Reverse Triggered Animation If Method Fails Using Xaml, Not Code Behind

我有一个带按钮的登录表单 (btnLogin)。当用户单击 btnLogin 时,将触发以下 Storyboard 以禁用 btnLogin, txtUsername, pbPassword ...

<EventTrigger RoutedEvent="Button.Click">
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                </BooleanAnimationUsingKeyFrames>
                <BooleanAnimationUsingKeyFrames Storyboard.TargetName="txtUsername" Storyboard.TargetProperty="IsEnabled" FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                </BooleanAnimationUsingKeyFrames>
                <BooleanAnimationUsingKeyFrames Storyboard.TargetName="pbPassword" Storyboard.TargetProperty="IsEnabled" FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>

... 当此 Storyboard 运行时,Button.Click 事件也附加到验证用户凭据的 btnLogin_Click 方法。很公平!

目前,如果登录尝试失败,我必须在 btnLogin_Click 中使用以下(肮脏的?!)代码来重新启用 btnLogin、txtUsername、pbPassword 控件,以便可以重新输入详细信息...

btnLogin.Triggers.Clear();
btnLogin.BeginAnimation(UIElement.IsEnabledProperty, null);
txtUsername.BeginAnimation(UIElement.IsEnabledProperty, null);
pbPassword.BeginAnimation(UIElement.IsEnabledProperty, null);

我可以使用 Xaml 来处理这个重新启用吗?我确定我可以,我只是不知道该怎么做?!

感谢您的帮助:O)


更新

我试过按照 bars222 的建议使用 EnterActionsExitActions,但是这种方法无法确定 btnLogin_Click 事件导致登录成功还是失败。

如果我清楚地理解,你想在登录尝试后将 IsEnabled 设置为 true。在这种情况下,您可以使用 EventTrigger.EnterActionsEventTrigger.ExitActions 触发器。这里有一些例子。

<EventTrigger RoutedEvent="Button.Click">
    <EventTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.EnterActions>
    <EventTrigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.ExitActions>
</EventTrigger>

更新

数据绑定解决方案。你可以创建一些属性,表示授权过程。您应该在身份验证过程中更新它。

private bool _isAuth;
public bool IsAuth
{
    get { return _isAuth; }
    set { _isAuth = value; NotifyPropertyChanged( "IsAuth" ); }
}

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
protected void NotifyPropertyChanged( String info )
{
    if ( PropertyChanged != null )
    {
        PropertyChanged( this, new PropertyChangedEventArgs( info ) );
    }
}

您应该设置 DataContext 这些元素。例如,如果您在授权 window 中创建了 属性。你可以写在构造函数中。

this.DataContext = this;

并将它绑定到您的元素 enable/disable 它。

<Button Click="Button_Click" IsEnabled="{Binding IsAuth}" />