如何从其Click事件WPF停止执行按钮命令 [英] How to stop the Button Command execution from its Click event WPF

查看:133
本文介绍了如何从其Click事件WPF停止执行按钮命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于单击事件的某些条件,是否有任何方法可以停止执行Button命令?

Is there any way to stop the Button command execution based on some condition on its click event?

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

Actually I want to have confirmation popup on some of the button click in our application. To make it a general solution, I have done the following:

  1. 我扩展了WPF按钮类调用AppBarButton的扩展,该类已经包含一些依赖项属性.
  2. 我对此还有2个属性,如下所示:IsActionConfirmationRequiredConfirmationActionCommand.
  1. I have extension of WPF button class call AppBarButton which already contains some dependency properties.
  2. I have 2 more properties for this as below: IsActionConfirmationRequired and ConfirmationActionCommand.

如果IsActionConfirmationRequired,则在单击左键单击事件时,我将打开确认弹出窗口.

If IsActionConfirmationRequired then on left button cick event I am opening the confirmation popup.

有什么方法可以避免创建新的ConfirmationActionCommand,并使用与Button相同的Command属性?如果我设置了Command,然后在Button上单击,即使用户未确认仍然执行操作静止按钮命令,也遇到了问题.

Is there any way to avoid creating new ConfirmationActionCommand, and use the same Command property of the Button? The problem I am getting if I set Command, then on Button click even if user not confirmed the action still button command execute.

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.

Please suggest something. I am not able to find anything. Already tried CanExecute to false, but its make button disable so no use. I just simple dont want make another command, and developer who will use this AppBarButton need to set ConfirmationActionCommand not normal Command.

推荐答案

如果我正确理解,您想确认用户的操作并执行存储在ButtonBase.Command属性中的命令.

If I correctly understand, you want to confirm user's action and execute a command which is stored in the ButtonBase.Command property.

为此,请删除ConfirmationActionCommand属性,并使用OnClick方法而不是Click事件.在重写的OnClick方法中,调用基本方法,该方法将在用户确认操作或不需要确认的情况下从Command属性执行命令.

To achieve that remove the ConfirmationActionCommand property and use the OnClick method instead of the Click event. In the overriden OnClick method call the base method which will execute a command from the Command property if an user confirmed an action or there is no confirmation required.

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();
        }
    }
}

这篇关于如何从其Click事件WPF停止执行按钮命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆