Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PreviewMouseLeftButtonDown and MouseLeftButtonDown WPF [duplicate]

Tags:

c#

events

wpf

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!

like image 889
gkb Avatar asked Dec 25 '22 06:12

gkb


2 Answers

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.

like image 129
Sheridan Avatar answered Dec 27 '22 20:12

Sheridan


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

like image 35
Sajeetharan Avatar answered Dec 27 '22 19:12

Sajeetharan