views:

38

answers:

2

Hello,

It appears that the args object passed into an override of OnPreviewMouseLeftButtonDown describes the current (live) mouse button state, not a snapshot of the state present when the event occurred.

Is this proper behavior? Shouldn't event arguments reflect event data at the moment the event occurred (a snapshot) and not be automatically updated to reflect live data?

Thank you,
Ben


Code Sample

protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
   // before MessageBox display, e.ButtonState = Pressed
   MessageBox.Show("OnPreviewMouseLeftButtonDown");

   // now, e.ButtonState = Released 
   base.OnPreviewMouseLeftButtonDown(e);
}
A: 

Any instance members of EventArgs class are not guaranteed to be thread safe. So I guess we can not complain if the internal state of the argument changes ;)

Bharath K
+1  A: 

I found out why e.ButtonState's value changes in the event handler--each time that property is accessed, a call is made to an underlying MouseDevice which returns the current (live) button state vs. the state that existed the moment the event occurred. (Thanks to Microsoft's Bob Bao for pointing this out.)

I've blogged about this at http://bengribaudo.com/blog/2010/07/26/38/event-arguments-static-snapshots.

Ben Gribaudo