views:

1472

answers:

5

Given that you have a control that fires a command:

<Button Command="New"/>

Is there a way to prevent the command from being fired twice if the user double clicks on the command?

EDIT: What is significant in this case is that I am using the Commanding model in WPF.

It appears that whenever the button is pressed, the command is executed. I do not see any way of preventing this besides disabling or hiding the button.

+2  A: 

Perhaps the button should disabled after the first click and until the processing has been completed?

vidalsasoon
I believe this is essentially what I will need to do.
Josh G
+1  A: 

You could set a flag

bool boolClicked = false;
button_OnClick
{
    if(!boolClicked)
    {
        boolClicked = true;
        //do something
        boolClicked = false;
    }
}
Nathan Koop
A: 

If your control derives from System.Windows.Forms.Control, you can use the double click event.

If it doesn't derive from System.Windows.Forms.Control, then wire up mousedown instead and confirm the click count == 2 :

private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
       //Do stuff
    }
 }
Noel Kennedy
This is a WPF control... It derives from System.Windows.Controls.Control
Josh G
Then you can use this:http://msdn.microsoft.com/en-us/library/system.windows.input.mousebuttoneventargs.clickcount.aspxOr have I missunderstood?
Noel Kennedy
The goal was not to handle the double click event; my intention is to suppress it.
Josh G
+1  A: 

Assuming that WPF Commanding doesn't give you enough control to mess with the click handler, could you put some code in the command handler that remembers the last time the command was executed and exits if it is requested within a given time period? (code example below)

The idea is that if it's a double-click, you'll receive the event twice within milliseconds, so ignore the second event.

Something like: (inside of the Command)


// warning:  I haven't tried compiling this, but it should be pretty close
DateTime LastInvoked = DateTime.MinDate;
Timespan InvokeDelay = Timespan.FromMilliseconds(100);
{
  if(DateTime.Now - LastInvoked <= InvokeDelay)
     return;

  // do your work
}

(note: if it were just a plain old click handler, I'd say follow this advice: http://blogs.msdn.com/oldnewthing/archive/2009/04/29/9574643.aspx )

JMarsch
Good link. The debouncing technique that your refer to is precisely what I am talking about. Turns out our WPF program misperforms sometimes if you double click on buttons that were intended to be singly clicked.
Josh G
A: 

This checks if validation has passed and if it does then disables the button.

private void checkButtonDoubleClick(Button button)
    {
        System.Text.StringBuilder sbValid = new System.Text.StringBuilder();
        sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
        sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
        sbValid.Append("this.value = 'Please wait...';");
        sbValid.Append("this.disabled = true;");
        sbValid.Append(this.Page.ClientScript.GetPostBackEventReference(button, ""));
        sbValid.Append(";");
        button.Attributes.Add("onclick", sbValid.ToString());
    }
ta4ka
Wow. Not sure what's going on here, but looks like you are assuming ASP.NET or something. There is no Page, no ClientScript, and no post back.
Josh G
Yes, wrong place to post this. This is for standard asp.net, not wpf.
ta4ka