5

While learning WPF (I am new to this), I created a simple Window and put one TextBox for entering username. I put some Text value initially in this TextBox (say Username). I wanted this text to disappear as soon as MouseLeftButtonDown is fired. Below is my xaml and C# code-

<TextBox Name="usernameTextBox" Background="Transparent" PreviewMouseLeftButtonDown="usernameTextBox_PreviewMouseLeftButtonDown"  HorizontalAlignment="Left" Height="23" Margin="10,103,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="166" Text="Username" />

C# code

private void usernameTextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     if (usernameTextBox.Text.ToLower() == "username")         
        usernameTextBox.Text = "";                       
}

This however, didn't work. After some search, I came across this SO question. And PreviewMouseLeftButtonDown event worked as expected.

So my question is, what is the difference between these two events and how would I know when to use one and when to use the other?

Thanks!

Community
  • 1
  • 1
gkb
  • 1,449
  • 2
  • 15
  • 30
  • It's a routed event, so it has different phases: http://msdn.microsoft.com/en-us/library/ms742806(v=vs.110).aspx - check out the bit on Tunnelling and Bubbling later on - `Preview` events are the tunnelling part of the cycle, and the other part is the bubbling part – Charleh Jul 25 '14 at 11:49

4 Answers4

7

Other Microsoft technologies like Windows Forms have standard CLR events. These are described as:

Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events.

For WPF, Microsoft have introduced RoutedEvents, with three separate Routing Strategies As always, Microsoft has the best explanations of these different strategies (from the linked page):

Bubbling: Event handlers on the event source are invoked. The routed event then routes to successive parent elements until reaching the element tree root. Most routed events use the bubbling routing strategy. Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements.

Direct: Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events. However, unlike a standard CLR event, direct routed events support class handling (class handling is explained in an upcoming section) and can be used by EventSetter and EventTrigger.

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

In simplest terms though, the Tunneling events, always with names starting with Preview, occur before Bubbling events and so are preferable to handle. The actual derived EventArgs object that a RoutedEvent uses is shared between both the Tunneling and the relating Bubbling events. If an event has a related Tunneling event, you can be certain that an attached handler will be called, whereas some controls set the Tunneling event as Handled, so the relating Bubbling event is never called.

Please see the linked page for the full details about Routed Events.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • A cool thing is that you may actually add handlers that gets the events, regardless if they are handled or not. Nice if you forinstance require KB, Mouse or Touch routed events inside whatever control to determine inactivity. – Stígandr Jul 25 '14 at 12:11
  • 3
    -1.`Bubbling events occur before Tunneling events and so are preferred.` - That's not right. Tunnelling(Preview) events occurs before bubbling events always. – Rohit Vats Jul 25 '14 at 18:57
  • 3
    @RohitVats +1 to your comment my friend. You are of course correct, I'm not quite sure what I was thinking at the time. I have corrected my mistake and further clarified the explanation. Thank you for pointing that out for me. – Sheridan Jul 25 '14 at 20:42
  • Reverted the downvote.. :) – Rohit Vats Jul 26 '14 at 07:49
2

These are all Routed Events.PreviewMouseLeftButtonDown and MouseLeftButtonDown are a pair of routed events used to notify elements in the visual tree that the user has depressed the left mouse button

Check here:

PreviewMouseLeftButtonDown and MouseLeftButtonDown

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

A typical WPF application contains many elements. Whether created in code or declared in XAML, these elements exist in an element tree relationship to each other. The event route can travel in one of two directions depending on the event definition, but generally the route travels from the source element and then "bubbles" upward through the element tree until it reaches the element tree root (typically a page or a window)

 MouseLeftButtonDown is a bubbles type event.

More detail: http://msdn.microsoft.com/en-us/library/ms742806.aspx

Preview events, also known as tunneling events, are routed events where the direction of the route travels from the application root towards the element that raised the event.

More in http://msdn.microsoft.com/en-us/library/ms752279.aspx

Ugur
  • 1,257
  • 2
  • 20
  • 30
1

The PreviewMouseLeftButtonDown routed event is called before the MouseLeftButtonDown routed event. You may forinstance cancel the whole event if you want to.

Stígandr
  • 2,874
  • 21
  • 36