c# - How to stop the Button Command execution from its Click event WPF -
is there way stop button command execution based on condition on click event?
actually want have confirmation popup on of button click in our application. make general solution, have done following:
- i have extension of wpf button class call
appbarbutton
contains dependency properties. - i have 2 more properties below:
isactionconfirmationrequired
,confirmationactioncommand
.
if isactionconfirmationrequired
on left button cick event opening confirmation popup.
is there way avoid creating new confirmationactioncommand
, , use same command
property of button
? problem getting if set command
, on button
click if user not confirmed action still button command execute.
c#:
public class appbarbutton : button { public appbarbutton() { this.click += appbarbutton_click; } private void appbarbutton_click(object sender, routedeventargs e) { button button = sender button; if (button == null || isactionconfirmationrequired == false || confirmationactioncommand == null) return; const string defaultmessage = "do want {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 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 flag show confirmation popup on before taking action on app bar button click. /// required set command in tag property of app bar button not in command property, required command fire when user /// confirms , click on action button on confirmation popup. /// </summary> public bool isactionconfirmationrequired { { return (bool)getvalue(isactionconfirmationrequiredproperty); } set { setvalue(isactionconfirmationrequiredproperty, value); } } /// <summary> /// gets or sets confirmation message in confirmation popup before taking action on app bar button click. /// </summary> public icommand confirmationactioncommand { { 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}" />
please suggest something. not able find anything. tried canexecute
false, make button disable no use. simple dont want make command, , developer use appbarbutton
need set confirmationactioncommand
not normal command
.
if correctly understand, want confirm user's action , execute command stored in buttonbase.command
property.
to achieve remove confirmationactioncommand
property , use onclick
method instead of click
event. in overriden onclick
method call base method execute command command
property if user confirmed action or there no confirmation required.
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 { { return (bool)getvalue(isactionconfirmationrequiredproperty); } set { setvalue(isactionconfirmationrequiredproperty, value); } } public string actionconfirmationmessage { { 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 set dialogresult property in confirmationdailog class // confirmed = dialog.showdialog().value; } // if there no confirmation requred or if user have confirmed action // call base method execute command if exists. if (confirmed) { base.onclick(); } } }
Comments
Post a Comment