【问题标题】:How to stop the Button Command execution from its Click event WPF如何从其 Click 事件 WPF 中停止执行按钮命令
【发布时间】:2015-10-22 14:15:03
【问题描述】:

有什么方法可以根据点击事件的某些条件停止 Button 命令的执行?

实际上,我希望在我们的应用程序中单击某些按钮时弹出确认消息。为了使其成为通用解决方案,我做了以下工作:

  1. 我有 WPF 按钮类调用 AppBarButton 的扩展,它已经包含一些依赖属性。
  2. 我还有 2 个属性如下:IsActionConfirmationRequiredConfirmationActionCommand

如果IsActionConfirmationRequired 然后在左键点击事件中,我将打开确认弹出窗口。

有什么方法可以避免创建新的ConfirmationActionCommand,并使用Button 的相同Command 属性?如果我设置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

【问题讨论】:

  • 您好,您是否尝试过将 click 事件绑定到您的 xaml.cs 中,您可以通过它们检查按钮上的属性并调用基本点击事件。
  • 只有当对话框返回true时才显示ConfirmationDailog并执行Button.Command

标签: c# wpf button popup command


【解决方案1】:

如果我理解正确,您想确认用户的操作并执行存储在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();
        }
    }
}

【讨论】:

  • 非常感谢哟..它的工作非常好...解决了我的问题...我不知道为什么我没有想到这个方向...:P Superlike for solution...我不知道我该如何投票给你这个解决方案......非常感谢......
  • 您可以通过点击帖子左侧的两个箭头之一来投赞成票或投反对票。您也可以接受有关您的问题的答案。要了解更多信息,请拨打Tour 并访问Help Center
  • 酷.. 完成.. 再次感谢您.. 您的快速回复并发布了如此好的解决方案... :) :)
  • @YohDeadfall:如何将其用于RadListBox SelectionChanged 事件?它要求参数。 base.OnSelectionChanged(//parameter here)。有什么想法吗?
  • @SanthoshKumar 我从未使用过 Telerik 控件,但可能您应该将 OnSelectionChanged 覆盖的 e 参数传递给基本方法。
【解决方案2】:

处理PreviewMouseLeftButtonDown 事件,而不是单击事件。这发生在命令执行之前。在处理程序中要求确认并将eventHandled 属性设置为true 或false。
如果设置为true,则不会执行该命令。

private void btn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var confirmed = true;   //use your confirmation instead
    e.Handled = confirmed;
}

您的代码如下所示(我不知道具体是因为我没有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;
    }
    ...

【讨论】:

  • 不好。这不是点击,而是按下按钮。仅在释放按钮时才会发生单击。用户知道并期待它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 2022-11-21
  • 2020-11-15
  • 1970-01-01
相关资源
最近更新 更多