有没有办法根据Click事件的某些条件停止执行Button命令?
实际上我想在我们的应用程序中点击一些按钮确认弹出窗口。为了使其成为一般解决方案,我做了以下工作:
AppBarButton
的扩展,它已经包含一些依赖属性。IsActionConfirmationRequired
和ConfirmationActionCommand
。如果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 :(得分: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();
}
}
}
答案 1 :(得分:2)
代替click事件,处理PreviewMouseLeftButtonDown
事件。这在命令执行之前发生。在处理程序中请求确认并将Handled
的{{1}}属性设置为true或false
如果将其设置为true,则不会执行该命令。
event
以下是您的代码的外观(我完全不知道,因为我没有private void btn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var confirmed = true; //use your confirmation instead
e.Handled = confirmed;
}
的代码:
ConfirmationDailog