gpt4 book ai didi

c# - 如何从其 Click 事件 WPF 中停止按钮命令的执行

转载 作者:太空狗 更新时间:2023-10-29 23:06:28 24 4
gpt4 key购买 nike

有什么方法可以根据点击事件的某些条件来停止 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

最佳答案

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

关于c# - 如何从其 Click 事件 WPF 中停止按钮命令的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33283210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com