Apr 26

After installing the Silverlight templates, I”ve started learning it by trying to build a new button control (the choice may seem strange, but currently Silverlight doesn”t have any controls). Well, if you create a new project based on the template, you”ll see that it already has code that reproduces the basic behavior of a button. After looking at the code, I thought that most of the button”s behavior could be built directly from XAML (at least, that”s what i thought then). Let me explain this a little better: the default code generated by the project template has javascript code for handling the mouse enter/leave events which changes the offset of a linear gradient brush that is applied to fill the interior of the rectangle. Oh these MS guys…why, oh why did they write that js code when you have routed events and triggers??? So, with this basic WPF knowledge, I decided to remove the js references and add two triggers: onde for the Rectangle.MouseEnter event and another  for the Rectangle.MouseLeave event. Each has its own storyboard, where a single DoubleAnimation refreshes the Offset values of one of the gradients (ie, it simply mimics the js default code,but it uses animations inside storyboards which respond to events -or,at least that”s what i thought it would do).

guess what: it simply didn”t work. I tried replacing the events for mouse down/up and again it didn”t work. In fact, i simply couldn”t see anything! It was only when i changed the duration of the animation  that i was able to see what”s going on: the animations were being run in sequence, during the loading of the controls! Yes, that”s right: even though I”ve set the events to enter/leave and mouse up/down, the damn thing only worked during the loaded event (at least, that”s what it seemed)…

After reading the docs, I”ve finally found something similar here. I”ve copied/pasted it and guess what? It doesn”t work! Interestingly, if you adapt it and put it in xaml pad, it works as expected (so does my initial button code)

Now, the big question: why doesn”t it work? aren”t we suppose to have routed events? or am I doing something wrong?



5 comments so far

  1. Dave Reed
    5:55 pm - 4-26-2007

    Silverlight doesn”t support triggers like other than ”loaded”. To get that behavior your have to listen to the events in code and fire the animations that way.

  2. Luis Abreu
    7:26 pm - 4-26-2007

    Hello Dave.
    thanks for the info. If you”re reading this, 2 other questions:
    1. are there any plans for supporting them in the final release?
    2. maybe you should put that info in the docs because I didn”t find anything out there that says what you”ve just confirmer (in fact, the link i”ve provided shows the exact opposite behavior)

  3. Dave Reed
    8:02 pm - 4-27-2007

    1. I don”t know one way or the other so I can”t really comment on this, sorry.
    2. Its definitely an error in the docs, only the Loaded event can be a routed event in Silverlight. I”m also not familiar with what docs there may be out there, but I”m sure docs will be improving with the next iteration.

    So in short I”m not all that helpful 🙂

  4. Luis Abreu
    8:28 pm - 4-27-2007

    on trhe contrary! You”ve confirmed the most important thing: there is only a routed event in the current Silverlight version: the Loaded event.

    thanks again.

  5. Yuvi
    2:55 pm - 5-2-2007

    Thanks guys! You just saved me a day of trying to figure out why the Triggers collection was there…