2015-10-22 126 views
3

有沒有什麼辦法來停止按鈕命令執行基於它的點擊事件的一些條件?如何停止按鈕命令執行從其Click事件WPF

其實我想要在我們的應用程序中點擊一些按鈕的確認彈出窗口。使之成爲通用的解決方案,我也做了以下內容:

  1. 我有WPF按鈕級呼叫AppBarButton裏面已經包含了一些依賴屬性的擴展。
  2. 我對此有2個屬性如下:IsActionConfirmationRequiredConfirmationActionCommand

如果IsActionConfirmationRequired然後左鍵cick事件我打開確認彈出窗口。

有什麼辦法可以避免創建新的ConfirmationActionCommand,並使用Command屬性Button?如果我設置了Command,那麼我得到的問題,然後在Button點擊即使用戶未確認動作仍然按鈕命令執行。

C#:

public class AppBarButton : Button 
{ 
    public AppBarButton() 
    { 
     this.Click += AppBarButton_Click; 
    } 

    private void AppBarButton_Click(object sender, RoutedEventArgs e) 
    { 
     Button button = sender as Button; 
     if (button == null || IsActionConfirmationRequired == false || ConfirmationActionCommand == null) 
      return; 

     const string defaultMessage = "Do you really want to {0}"; 

     string confirmationPopUpMessage = string.IsNullOrEmpty(ActionConfirmationMessage) 
      ? DebugFormat.Format(defaultMessage, button.Content) 
      : ActionConfirmationMessage; 

     ConfirmationDailogDetails confirmationDailogDetails = new ConfirmationDailogDetails 
     { 
      Message = confirmationPopUpMessage, 
      ActionName = button.Content.ToString(), 
      Template = button.Template, 
      ActionCommand = ConfirmationActionCommand 
     }; 

     //instead of ConfirmationActionCommand want to use base.Command 
     ConfirmationDailog dialog = new ConfirmationDailog(confirmationDailogDetails) 
     { 
      PlacementTarget = button, 
      IsOpen = true 
     }; 
    } 

    public static readonly DependencyProperty IsActionConfirmationRequiredProperty = 
     DependencyProperty.Register("IsActionConfirmationRequired", typeof(bool), typeof(AppBarButton)); 

    public static readonly DependencyProperty ActionConfirmationMessageProperty = 
     DependencyProperty.Register("ActionConfirmationMessage", typeof(string), typeof(AppBarButton)); 

    public static readonly DependencyProperty ConfirmationActionCommandProperty = 
     DependencyProperty.Register("ConfirmationActionCommand", typeof(ICommand), typeof(AppBarButton)); 

    /// <summary> 
    /// Gets or sets the flag to show the confirmation popup on before taking any action on App Bar button click. 
    /// Also its required to set the command in Tag Property of the App Bar button not in the Command Property, then only required command will fire only when user 
    /// confirms and click on the action button on confirmation popup. 
    /// </summary> 
    public bool IsActionConfirmationRequired 
    { 
     get { return (bool)GetValue(IsActionConfirmationRequiredProperty); } 
     set { SetValue(IsActionConfirmationRequiredProperty, value); } 
    } 

    /// <summary> 
    /// Gets or sets the confirmation message in confirmation popup before taking any action on App Bar button click. 
    /// </summary> 
    public ICommand ConfirmationActionCommand 
    { 
     get { return (ICommand)GetValue(ConfirmationActionCommandProperty); } 
     set { SetValue(ConfirmationActionCommandProperty, value); } 
    } 
} 

XAML:

<controls:AppBarButton x:Key="WithdrawAll" 
         AppBarOrder="1" 
         PageIndex="1" 
         AppBarOrientation="Left" 
         Content="Withdraw" IsActionConfirmationRequired="True" 
         ConfirmationActionCommand="{Binding WithdrawAllCommand}" 
         Template="{StaticResource DeleteCircleIcon}" /> 

請提出好的建議。我無法找到任何東西。已經嘗試CanExecute爲false,但其make按鈕禁用,因此沒有用處。我只是簡單的不想做另一個命令,並且開發人員會使用這個AppBarButton需要設置ConfirmationActionCommand不正常Command

+0

嗨,你有沒有嘗試將點擊事件綁定到你的xaml.cs,從他們你可以檢查按鈕上的屬性並調用基點擊事件。 – Chris

+1

只有當對話框返回「true」時,你纔想顯示'ConfirmationDailog'並執行'Button.Command'? –

回答

3

如果我理解正確的,你要確認用戶的行爲和執行命令,它被存儲在ButtonBase.Command屬性。

爲了實現這一除去ConfirmationActionCommand屬性並使用OnClick方法而不是Click事件。在重寫OnClick方法調用基本方法,如果用戶確認某個操作或不需要確認,該方法將執行Command屬性的命令。

public class AppBarButton : Button 
{ 
    public static readonly DependencyProperty IsActionConfirmationRequiredProperty = 
     DependencyProperty.Register("IsActionConfirmationRequired", typeof(bool), typeof(AppBarButton)); 

    public static readonly DependencyProperty ActionConfirmationMessageProperty = 
     DependencyProperty.Register("ActionConfirmationMessage", typeof(string), typeof(AppBarButton)); 

    public bool IsActionConfirmationRequired 
    { 
     get { return (bool)GetValue(IsActionConfirmationRequiredProperty); } 
     set { SetValue(IsActionConfirmationRequiredProperty, value); } 
    } 

    public string ActionConfirmationMessage 
    { 
     get { return (string)GetValue(ActionConfirmationMessageProperty); } 
     set { SetValue(ActionConfirmationMessageProperty , value); } 
    } 

    protected override void OnClick() 
    { 
     bool confirmed = true; 

     if (IsActionConfirmationRequired) 
     { 
      ConfirmationDailogDetails confirmationDailogDetails = new ConfirmationDailogDetails 
      { 
       Message = confirmationPopUpMessage, 
       ActionName = button.Content.ToString(), 
       Template = button.Template, 
       ActionCommand = ConfirmationActionCommand 
      }; 

      ConfirmationDailog dialog = new ConfirmationDailog(confirmationDailogDetails) 
      { 
       PlacementTarget = button, 
       IsOpen = true 
      }; 

      // Set confirmed here 

      // If you set the DialogResult property in the ConfirmationDailog class then 
      // confirmed = dialog.ShowDialog().Value; 
     } 

     // If there is no confirmation requred or if an user have confirmed an action 
     // then call base method which will execute a command if it exists. 
     if (confirmed) 
     { 
      base.OnClick(); 
     } 
    } 
} 
+0

謝謝你這麼多Yoh公司。其工作完全正常......解決我的問題。我不知道爲什麼我沒有在這個方向想...:P爲Superlike解決方案...我不知道我該怎麼投票您爲此解決方案....非常感謝您。 – Neha

+0

您可以通過單擊帖子左側的兩個箭頭之一來投票或向下投票。你也可以接受你的問題的答案。要了解更多信息,請參閱[Tour](http://stackoverflow.com/tour)並訪問[Help Center](http://stackoverflow.com/help)。 –

+0

酷..做..再次..非常感謝你曾經爲你的快速回復,併發布了這樣一個很好的解決方案... :) :) – Neha

2

而不是點擊事件,處理PreviewMouseLeftButtonDown事件。這發生在命令執行之前。在處理程序中請求確認並將eventHandled屬性設置爲true或false。
如果將其設置爲true,則不會執行該命令。

​​

這裏是你的代碼可能看起來怎麼樣(我不知道到底是因爲我沒有爲ConfirmationDailog代碼:

public class AppBarButton : Button 
{ 
    public AppBarButton() 
    { 
     this.PreviewMouseLeftButtonDown += AppBarButton_PreviewMouseLeftButtonDown; ; 
    } 

    private void AppBarButton_PreviewMouseLeftButtonDown(object sender, RoutedEventArgs e) 
    { 
     Button button = sender as Button; 
     if (button == null || IsActionConfirmationRequired == false || ConfirmationActionCommand == null) 
      return; 

     const string defaultMessage = "Do you really want to {0}"; 

     string confirmationPopUpMessage = string.IsNullOrEmpty(ActionConfirmationMessage) 
      ? DebugFormat.Format(defaultMessage, button.Content) 
      : ActionConfirmationMessage; 

     ConfirmationDailogDetails confirmationDailogDetails = new ConfirmationDailogDetails 
     { 
      Message = confirmationPopUpMessage, 
      ActionName = button.Content.ToString(), 
      Template = button.Template, 
      ActionCommand = ConfirmationActionCommand 
     }; 
     **//instead of ConfirmationActionCommand want to use base.Command** 
     ConfirmationDailog dialog = new ConfirmationDailog(confirmationDailogDetails) 
     { 
      PlacementTarget = button, 
      IsOpen = true 
     }; 
     //validation here 
     dialog.ShowDialog(); 
     var confirmed = dialog.IsConfirmed; 
     e.Handled = confirmed; 
    } 
    ... 
+0

不好。這不是一個點擊,而是一個按鈕。點擊僅在按鈕被釋放時發生。用戶知道並期待它。 – ygoe