Adding double click support in Silverlight

Posted in Programming,Silverlight by ary on the June 24th, 2008

Silverlight Beta 2 doesn’t support double click, but since I needed it, I implemented it. I’ve created a class named Mouse. You create a Mouse instance that wraps an UIElement. The instance attaches itself to the MouseLeftDownButton and, using a timer, it allows you to recieve events when double click is performed.

Actually, you can detect as many clicks as you wish: the event arguments include the number of clicks performed by the user.

But I don’t want to attach to mouse events in code. I want to do it in XAML, like with MouseLeftButtonDown or Click. But, unfortunately, you can’t use attached properties whose type is an event handler. If you do that, you get an ugly exception.

“What a pity”, I thought, “now I need to remove some of my MouseLeftButtonDown events and replace them by code that creates Mouse instances and then attaching to Mouse events programatically”. But before doing that, I decided to give it another shot. What if I created an attached property whose type is string, and then at runtime I searched the method and invoke it?

The problem with that approach is that the method won’t be declared in the object where you are attaching the event, but probably in some parent control. For example:

<UserControl>
    <Rectangle manas:Mouse.Click="SomeMethod" />
</UserControl>

SomeMethod is probably declared in our custom UserControl, not in Rectangle. But that poses no problem at all: just go up in the visual hierarchy throught the Parent property until we find a class that declares SomeMethod with the signature of our interest.

But, alas, at the time attached properties are processed by the XAML processor, the object in question is not yet in the visual hierarchy: it doesn’t have a parent. That’s no problem at all: we attach to the Loaded event and do the lookup in that moment, and we are guaranted that we can reach the parent we are interested in.

Once I did that I was really amazed that it worked! :-)

Then, I refactored the code so I could use that trick to support other custom event handlers in XAML as well. Finally, changing the old event handlers to start firing at double clicks instead of single clicks was a matter of seconds.

Here’s the full code in case someone finds it useful.

(+) Show Code

Google translate curiosity

Posted in Languages by ary on the June 5th, 2008

I tend to have phrases in many languages (Spanish, English, French, Esperanto) in my instant messenger, and my coworkers always ask me what they mean. Today I have this french phrase:

Le voilà, le gros cochon, le voilà, le bon à rien! C’est du joli, c’est du joli!

(from the story Toine, from Guy de Maupassant) which means, more or less

Here it is, the big pig, here it is, the good for nothing! How nice, how nice!

But instead of saying that to my coworkers, I told them to use Google translate, which I did to check the result. And what a surprise:

There he is, the big pig, here it is, good for nothing! It’s pretty, it’s beautiful!

Notice how Google translated differently the words “C’est du joli”. I wonder if that’s because of the statistical translation (you know, feeding a lot of books to the machine) or because it tries to not repeat the same words over an over, much like what a person does when she writes.

Now, we wanted to see what happened if we repeated the words a lot of times:

Le voilà, le gros cochon, le voilà, le bon à rien! C’est du joli, c’est du joli, c’est du joli, c’est du joli, c’est du joli, c’est du joli, c’est du joli, c’est du joli, c’est du joli!

And again, to our surprise:

There he is, the big pig, here it is, good for nothing! It’s pretty, it’s pretty, it’s pretty, it’s pretty, it’s pretty, it’s pretty, it’s pretty, it’s nice, that east of beautiful!

Further, changing capital letter to small letters, or removing the exclamation mark gives you different results.