2015-11-05 49 views
0

我有一個帶有按鈕的登錄表單(btnLogin)。當用戶點擊btnLogin,以下Storyboard被觸發禁用btnLogin,txtUsername,pbPassword ...反向觸發動畫如果方法失敗使用Xaml,而不是代碼

<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)


UPDATE

我使用EnterActionsExitActions試圖通過bars222的建議,但這種做法有沒有辦法確定btnLogin_Click事件導致登錄成功或失敗。

回答

1

如果我清楚明白,您想在登錄嘗試後將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> 

更新

解數據綁定。您可以創建一些指示授權過程的屬性。你應該在auth過程中更新這個。

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。例如,如果您在授權窗口中創建屬性。你可以寫在構造函數中。

this.DataContext = this; 

並將其綁定到您的元素以啓用/禁用它。

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

此外,我認爲最好使用數據綁定爲你的情況,但它需要一些代碼背後。 – bars222

+0

感謝您的回覆。不幸的是,該方法根本不禁用按鈕。我認爲它可能會禁用,然後立即啓用它。你能給我提供關於你提到的數據綁定解決方案的更多信息嗎? –

+0

對於第一個答案,我建議授權表單將在成功驗證過程後被禁用。用數據綁定示例更新答案。 – bars222