Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

Who Ate My Mouse Down Event??

Before I leave, I want to squeeze in one quick note about a problem a lot of folks are running into as they upgrade from Beta 1 to Beta 2. I’ll come back to this in more detail when I get back, but here it is in a nutshell.

A breaking change in the Beta 2 upgrade is that all the controls now handle MouseLeftButtonDown/Up/Move, while graphic objects do not.

That means that no built in control will pass those events on (bubble them up) anymore. So, if you revisit my example for drag and drop what you’ll find is that if you create a button that has a graphic object inside it, and you drag on the button, it will not move, but if you drag on the graphic object it will! 

VLGMButton

You can see this in (the newly updated) Tutorial #1 (where the image for Drag and Drop is misleading). Dragging on the Very Little Green Man works, dragging on the button he is in, does not.

That is because the Very Little Green Man consists of nothing but Graphics (elipses and paths) but the button is a control and the control eats the event.

This is why figure 1.9 in the tutorial has to be replaced; you can’t drag the button, you have to drag the green man,

BrokenImage
Broken image

It’s subtle but important, where the mouse is shown, you could not be dragging. Yes, in Beta 1, no in Beta 2.

FixedImage
Fixed image

Here the mouse is dragging the little green man, and that will work just fine.

The full reason for this is to come, but the short explanation is that this helps controls in Silverlight behave more like controls in WPF.

Comments

Silverlight SDK said:

With Silverlight 2 Beta 2 coming out there were a number of changes that will break applications written

# June 12, 2008 5:33 PM

Feal Xu said:

Hi Liberty,

Many thanks in advance that I have learned so much knowledge about Silverlight 2 from your blog in the past days.

This is just a suggestion for this article that maybe you should assign this important notification in your updated tutorial 1 PDF. For me,it is really lucky to see this good article because I have read almost all your articles about silverlight 2, but for others, they might be very confused when they encountered this issue, especially for a beginner.

Go ahead your fantastic work! Wish you every success!

                                               Feal

# June 16, 2008 2:04 AM

jesseliberty said:

>>I have a HyperLinkButton in ItemsControl. Can't trap the click event.<<

Every control must have its own event handler; controls do not bubble their events.

# June 18, 2008 6:50 PM

yaip said:

So should I define MouseLeftButtonDown in ItemsControl or HyperLinkButton? Here is my xaml:

<ItemsControl x:Name="itmStudents"  Margin="0,0,0,0" Canvas.Top="63.287" Canvas.Left="172.2" Padding="10"  >

<ItemsControl.ItemTemplate>

<DataTemplate >

<HyperlinkButton x:Name="hlbButton" FontSize="16"   ></HyperlinkButton>

</DataTemplate>

</ItemsControl.ItemTemplate>

</ItemsControl>

and my code-behind:

   Private Sub itmStudents_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles itmStudents.MouseLeftButtonDown, itmStudents.MouseLeftButtonUp

       btnSubmit.Opacity = 0

       txtStudent.Opacity = 0

       lblStudent.Opacity = 0

       Me.itmStudents.Opacity = 0

       txtMessage.Text = "Please wait..."

       Dim textB As TextBlock = e.Source

       Dim linkB As HyperlinkButton = textB.DataContext

       Dim s As String = linkB.Tag

       Dim asmx2 As New ServiceReference2.GetStudentArt_WSSoapClient

       AddHandler asmx2.GetStudentArtCompleted, AddressOf asmx2_GetStudentArtWithAsmxCompleted

       asmx2.GetStudentArtAsync(Convert.ToInt32(linkB.Tag))

   End Sub

This was working fine in Beta 1.

# June 18, 2008 8:22 PM

Kieren5 said:

*Shakes head*

Short answer - to make it more like Wpf... well, we need a preview event to so we can get a lookin!

Without looking at reflecter, I can't tell when a nested control is going to handle an event that my container control should!

Please see this post:

"How to know whether an element handles a Routed Mouse EVent":

silverlight.net/.../21154.aspx

# July 23, 2008 10:19 AM