C# 如何从单击事件WPF停止按钮命令的执行

C# 如何从单击事件WPF停止按钮命令的执行,c#,wpf,button,popup,command,C#,Wpf,Button,Popup,Command,是否有任何方法可以根据单击事件的某些条件停止按钮命令的执行 实际上,我想在我们的应用程序中的一些按钮点击上有一个确认弹出窗口。为了使其成为一个通用解决方案,我做了以下工作: 我有WPF button类调用的扩展AppBarButton,它已经包含一些依赖属性 我还有两个属性,如下所示:IsActionConfirmationRequired和confirmationcommand 如果IsActionConfirmationRequired,则在左侧按钮cick事件中,我打开确认弹出窗口 有没有

是否有任何方法可以根据单击事件的某些条件停止按钮命令的执行

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

  • 我有WPF button类调用的扩展
    AppBarButton
    ,它已经包含一些依赖属性
  • 我还有两个属性,如下所示:
    IsActionConfirmationRequired
    confirmationcommand
  • 如果
    IsActionConfirmationRequired
    ,则在左侧按钮cick事件中,我打开确认弹出窗口

    有没有办法避免创建新的
    确认命令
    ,并使用与
    按钮
    相同的
    命令
    属性?如果我设置了
    命令
    ,然后在
    按钮
    上单击,即使用户未确认操作仍然按钮命令执行,我也会遇到问题

    C#:

    公共类AppBarButton:按钮
    {
    公共应用程序按钮()
    {
    这个.Click+=AppBarButton\u Click;
    }
    私有无效应用程序按钮单击(对象发送器,路由目标e)
    {
    按钮按钮=发送器为按钮;
    如果(按钮==null | | IsActionConfirmationRequired==false | | ConfirmationCommand==null)
    返回;
    const string defaultMessage=“是否确实要{0}”;
    string confirmationPopUpMessage=string.IsNullOrEmpty(ActionConfirmationMessage)
    ?调试格式.格式(默认消息,按钮.内容)
    :ActionConfirmationMessage;
    确认DailogDetails确认DailogDetails=新确认DailogDetails
    {
    消息=确认弹出消息,
    ActionName=button.Content.ToString(),
    模板=按钮。模板,
    ActionCommand=确认命令
    };
    //要使用base.Command而不是confirmationCommand
    ConfirmationDailog对话框=新建ConfirmationDailog(confirmationDailogDetails)
    {
    PlacementTarget=按钮,
    等参=真
    };
    }
    公共静态只读从属属性IsActionConfirmationRequiredProperty=
    DependencyProperty.Register(“IsActionConfirmationRequired”、typeof(bool)、typeof(AppBarButton));
    公共静态只读依赖属性ActionConfirmationMessageProperty=
    Register(“ActionConfirmationMessage”、typeof(string)、typeof(AppBarButton));
    公共静态只读依赖属性ConfirmationCommandProperty=
    DependencyProperty.Register(“确认命令”、typeof(ICommand)、typeof(AppBarButton));
    /// 
    ///获取或设置在对应用程序栏按钮单击执行任何操作之前显示确认弹出窗口的标志。
    ///此外,还需要在应用程序栏按钮的标记属性中设置命令,而不是在命令属性中设置命令,那么只有当用户
    ///确认并单击确认弹出窗口上的操作按钮。
    /// 
    需要公开文件确认
    {
    get{return(bool)GetValue(IsActionConfirmationRequiredProperty);}
    set{SetValue(IsActionConfirmationRequiredProperty,value);}
    }
    /// 
    ///在对应用程序栏按钮单击执行任何操作之前,获取或设置确认弹出窗口中的确认消息。
    /// 
    公共ICommand确认命令
    {
    获取{return(ICommand)GetValue(confirmationCommandProperty);}
    set{SetValue(confirmationCommandProperty,value);}
    }
    }
    
    XAML:

    
    

    请提出一些建议。我什么也找不到。已经尝试将
    CanExecute
    设置为false,但其make按钮已禁用,因此没有任何用处。我只是不想再做一个命令,而使用这个
    AppBarButton
    的开发者需要设置
    confirmationCommand
    非正常
    命令

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

    private void btn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var confirmed = true;   //use your confirmation instead
        e.Handled = confirmed;
    }
    
    下面是您的代码的样子(我不知道确切情况,因为我没有
    确认dailog
    的代码):

    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;
        }
        ...
    

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

    要实现这一点,请删除
    confirmationCommand
    属性,并使用
    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();
            }
        }
    }
    

    您好,您是否尝试过将单击事件绑定到xaml.cs中,您可以从中检查按钮上的属性并调用基本的单击事件。是否要显示
    确认dailog
    并执行
    按钮。仅当对话框返回
    true
    时,才执行命令
    ?非常感谢您..它工作非常好…解决了我的问题问题…我不知道为什么我没有朝这个方向思考…:P Superlike寻求解决方案…我不知道我如何才能投票给你这个解决方案…非常感谢你…你可以通过点击帖子左侧两个箭头中的一个来投赞成票或反对票。你也可以接受你问题的答案。要了解更多信息,请访问e、 。再次非常感谢..感谢您的快速回复并发布了这么好的解决方案…:):@YohDeadfall:将此应用于
    RadListBox
    SelectionChanged
    事件如何?它要求输入参数<代码>base.OnSe
    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;
        }
        ...
    
    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();
            }
        }
    }