Looking at Triggers and Actions

April 27th, 2009     |     View Comments

Like I’ve mentioned several times earlier, behaviors in Expression Blend 3 are made up of extensible Triggers, Actions, and Behaviors. What I really have not done is explain in greater conceptual detail what each of those pieces do and how far you can push our default implementation. In this post, let’s fix that by giving Triggers and Actions their long-awaited detailed overview.

In a Nutshell
If I had to sum up what triggers and actions do, the following sentence is it:

trigger_action

Pretty simple, right? Let me take a few steps back and look at a few examples. When I flip on a light switch switch, the light needs to turn on. When I click on a button, a sound needs to play. When I hit a certain time, the application should close. What I have just described are simple cause-and-effect relationships, and in a nutshell, Triggers and Actions are nothing more than digital manifestations of them. The cause (When) is controlled by the Trigger, and the effect (Do) is controlled by the Action:

image

All of the examples involving Actions that I have shown you over the past few blog posts follow a similar pattern:

behaviorsTabAssetLibrary

In the Blend UI, we suppress a lot of the details for you. You only drag and drop an Action onto an element, and the trigger is either automatically set as specified by the Action, or you can set it yourself. That seems simple…sort of!

Sneaky Like a Fox
What makes Triggers and Actions more interesting is how sneaky they actually can be. When you drag an Action onto an element in Blend, your Action is parented under this button:

applyingABehavior

What is worth looking into is what the Trigger and Action are looking at, and let’s assume the Trigger we are looking at is an EventTrigger. Dropping an Action onto the above button will set the Trigger’s source to that of the button itself, and it will also set your Action’s target to be that of the button as well:

 image

What you have the ability to do is set the Source to some other element besides your button.  Let’s say you have a checkbox you want to set as your EventTrigger’s Source instead.

With our current Behaviors implementation, you can simply change the Source of your Trigger to that of your checkbox very easily by setting the Trigger’s SourceName property to that of your checkbox:

image

This means that your Action will do something when the event you are listening for on your CheckBox fires.

You’ve just seen how we can set the source of a trigger, our EventTrigger, to that of another element besides the one your Action is parented under. To make things a bit more interesting, you also have the ability to target your Action to affect an element other than the one  you are parented under as well!

If your Action inherits from TargetedTriggerAction instead of just TriggerAction, you gain access to the TargetName property that allows you to specify which element you want your Action to affect. So you can have something such as the following:

image

Your Action is parented under the Button, but your EventTrigger is actually listening for events from a CheckBox. When the trigger fires, your Action does its thing on a blue rectangle that it is targeting.The actual Button is merely the Action’s host and it derives no benefit from it because neither the Action nor the Trigger care about it. Sad, isn’t it?

Rationale Behind This
Right now, you are probably wondering why we allow you to do something like this. It isn’t as if you have to manually modify XAML for this. We provide UI today that allows you to easily do this. The reason has to to do with different approaches individuals have on how the relationship between a trigger and Action should work.

I tend to like the following approach where my Action is on the element that will be the Source of my trigger:

 image

This is known as the Tell approach because I am telling my Action to affect something else. The other approach is known as Listen, and it is where you place your Action on the element that is being affected as opposed to the Source of the trigger:

image

There is no right or wrong way to approach how you wire up your Triggers and Actions. The thing to note is that our Behaviors API is flexible enough to support both, and our UI makes it easy for you to pick either Listen or Tell without having to do anything crazy.

Cheers!
Kirupa :)

:

View Comments to “Looking at Triggers and Actions”

  1. Bozidar Says:

    Hi,
    I appologiese for posting this here, but I just wanted to say that code form http://www.kirupa.com/developer/actionscript/xmldataflash2.htm didn’t work. I fixed by replacing
    if(loaded) {….
    into
    if(xmlData.loaded) { …….
    I have Adobe Flash CS4.
    All best,
    Bozidar

    P.S. Your website is one of the best created and designed that I have seen. Thank you for it.

  2. Mansoor Says:

    Hi Kirupa,
    this is mansoor.can i have the code for hardrock memorabilla….

    Thanks,
    Mansoor

  3. vass Says:

    hi,
    this is vass … i hve gone through the material what u have enclosed in it was really good and esay 2 understand .. it help me to hve an quick idea … thx

  4. kirupaBlog - If it isn’t broken, take it apart and fix it! » Blog Archive » Looking at Behaviors, the Class Says:

    [...] my earlier article a few weeks ago, I described the basics of Triggers and Actions. Triggers and Actions satisfy your standard, run-of-the-mill, cause and effect condition where [...]

  5. kirupa Says:

    Hi Mansoor,
    The code for HardRock Memorabilia is not publically available, but you can get a basic overview of how to create a custom, filtered view using Deep Zoom here: http://blog.kirupa.com/?p=212

    Cheers!
    Kirupa :)

  6. Top-silverlight » Blog Archive » Looking at Behaviors, the Class Says:

    [...] my earlier article a few weeks ago, I described the basics of Triggers and Actions. Triggers and Actions satisfy your standard, run-of-the-mill, cause and effect condition where [...]

  7. kirupaBlog - If it isn’t broken, take it apart and fix it! » Blog Archive » Targeting Other Elements via the Blend 3 UI Says:

    [...] an earlier post, I described how you can change what your EventTrigger and Action (based on TargetedTriggerAction) [...]

  8. Jasom Says:

    I am just curious why not bother the trouble Re-Invent the wheel again? The WPF 3.5 already provide a framework for Trigger and TriggerAction. Basically every UIElement or DataTemplate/ControlTemplate can have a collection of Triggers and Each Trigger can have either Setters or TriggerActions when Trigger fire. Both Trigger and TriggerAction are abstract base class that user can extended. What you can do is fix the problem in current Trigger/Action implementation and provide a better UI in Blend to support it. I believe the native Trigger/Action is much more efficient then this one.

  9. kirupa Says:

    Hi Jasom – a large part of the reason had to do with providing Trigger/Action support in Silverlight. For WPF, we tried to be as close as possible to the existing Trigger and TriggerAction implementation, but there were some changes that needed to made.

    :)

  10. commercial proxy Says:

    I’m having a small problem I cannot subscribe your rss feed, I’m using google reader fyi.

  11. Dave Says:

    Great article and right to the point. I am not sure if this is really the best place to ask but do you people have any thoughts on where to hire some professional writers? Thanks :)

  12. Estefana Skinkle Says:

    This site called Unblock Websites also has some good information on the subject.

  13. debt relief Says:

    Of course, what a talented web website and enlightening posts, I’ll include backlink – save this webpage? Regards.

  14. music sites review Says:

    There is no right or wrong way to approach how you wire up your Triggers and Actions. The thing to note is that our Behaviors API is flexible enough to support both, and our UI makes it easy for you to pick either Listen or Tell without having to do anything crazy.

    Nice post.

  15. buy cheap music Says:

    Hi, I like your post. I was searching for the same. You have included everything in your blog but you can improve it a little bit.
    http://www.cheapmusicsites.com

  16. link wheel Says:

    I like your idea of sticking to one project at a time, although I once went into an interview and told the interviewer that my weakest link is that I have so many projects going on that it sometimes means some of them don?t get the proper attention. She s

  17. sim so dep Says:

    Thank you for the wise critique. Me & my neighbour have been getting ready to do a little analysis about that. We received an excellent guide on that matter from our local library and most books where not as influensive as your information. I am very glad to see such information which I used to be searching for a protracted time.This made very glad! Anyway, in my language, there aren’t a lot good source like this.

  18. south beach diet recipes Says:

    Took me time to read all the comments, however I really loved the article. It proved to be very useful to me and I’m positive to all of the commenters here! It’s all the time good when you can’t only learn, but in addition engaged! I am certain you had joy penning this article. Anyway, in my language, there aren’t much good source like this.

  19. dich vu seo Says:

    Utterly understand what your stance in this matter. Though I’d disagree on some of the finer particulars, I think you did an superior job explaining it. Certain beats having to analysis it on my own. Thanks. Anyway, in my language, there aren’t a lot good supply like this.

  20. Kristina Demarse Says:

    Have you ever considered about contributing on additional websites? You’ve some great content right here and I’m positive you can share bunch more in the event you wrote some content throughout other internet sites. You will find lots of associated websites to check out. Only a thing to consider. I’m glad I know about it at least.

  21. seo company Says:

    Great write-up, I seo company am a big believer in commenting seo company on blogs to assist the weblog seo company writers know that they?ve added one thing seo company worthwhile to the world large net! (source roblox-cheats.com). Anyway, seo company in my language, there aren’t a lot good seo company source like this.

  22. under sink water filter Says:

    Your knowledge truly fills a necessity. I have been wanting to find this type of article and you really came through. Can you imagine that everything you have written just about exactly duplicates my personal experience.

  23. Apryl Skerl Says:

    Are you saying that I can’t understand what you are posting?

  24. Wilbur Swickheimer Says:

    Good post, well put together. Thanks. I will be back soon to check out for updates. Cheers

  25. Paul Pline Says:

    Good post

  26. Leslee Kopps Says:

    The undergroun-d tour in Seattle – a city built on top of a city due to a fire! The buildings, etc. are still underneath and are available for tour. wow gold discount codes,cheapest wow gold servers,wow gold credit card

  27. Internet Marketing uk Says:

    What makes Triggers and Actions more interesting is how sly they actually can be. When you shift an Action onto a aspect in Mixture, your Action is parented under this choice.

  28. Twila Margaris Says:

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is magnificent, as well as the content!. Thanks For Your article about kirupaBlog & .

Leave a Reply

blog comments powered by Disqus