c# – 如何从Click事件WPF中停止Button Command的执行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何从Click事件WPF中停止Button Command的执行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5016字,纯文字阅读大概需要8分钟。
内容图文
![c# – 如何从Click事件WPF中停止Button Command的执行](/upload/InfoBanner/zyjiaocheng/809/02af00048fc84d9c9e79574dc46562fe.jpg)
有没有办法根据Click事件的某些条件停止Button命令的执行?
实际上我想在我们的应用程序中点击一些按钮确认弹出窗口.为了使其成为一般解决方案,我做了以下工作:
>我有WPF按钮类调用AppBarButton的扩展,它已经包含一些依赖属性.
>我还有2个属性,如下所示:IsActionConfirmationRequired和ConfirmationActionCommand.
如果IsActionConfirmationRequired然后在左按钮cick事件我打开确认弹出窗口.
有没有办法避免创建新的ConfirmationActionCommand,并使用Button的相同Command属性?如果我设置Command,我会得到的问题,然后按下按钮,即使用户没有确认动作仍然按钮命令执行.
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而不是正常的命令.
解决方法:
如果我正确理解,您需要确认用户的操作并执行存储在ButtonBase.Command属性中的命令.
要实现这一点,请删除ConfirmationActionCommand属性并使用OnClick方法而不是Click事件.在重写的OnClick方法中,调用base方法,如果用户确认操作或者不需要确认,它将从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();
}
}
}
内容总结
以上是互联网集市为您收集整理的c# – 如何从Click事件WPF中停止Button Command的执行全部内容,希望文章能够帮你解决c# – 如何从Click事件WPF中停止Button Command的执行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。