有沒有什麼辦法來停止按鈕命令執行基於它的點擊事件的一些條件?如何停止按鈕命令執行從其Click事件WPF
其實我想要在我們的應用程序中點擊一些按鈕的確認彈出窗口。使之成爲通用的解決方案,我也做了以下內容:
- 我有WPF按鈕級呼叫
AppBarButton
裏面已經包含了一些依賴屬性的擴展。 - 我對此有2個屬性如下:
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
。
嗨,你有沒有嘗試將點擊事件綁定到你的xaml.cs,從他們你可以檢查按鈕上的屬性並調用基點擊事件。 – Chris
只有當對話框返回「true」時,你纔想顯示'ConfirmationDailog'並執行'Button.Command'? –