Tag Property – The Diplomatic Pouch of Silverlight

    [updated 18:22 Eastern (utc-5) Oct 6.]

I received feedback from a few sources recently expressing confusion about the Tag property, which resides in the class FrameworkElement. 

Notice, first that Control (the base for all visible controls) derives directly from FrameworkElement and thus every control is-a Framework Element and thus every control has-a Tag. For free. No extra charge. Included. Gratis.

The Tag property is like a pocket you can stuff something into and be assured that it will be there when you need it later. It isn't type safe, but it is water-tight. This can be very handy (and can prevent a lot of needless sub-classing).

A tech reviewer on my book wrote that it can only hold strings, so I wrote what was supposed to be a quick and dirty program to show that, no, it can hold instances of classes. Worked great except for two problems:

  • Getting it to look nice took too much of the morning
  • After I was done I looked in the documentation and (as an unnamed vp candidate might say) "well shucks, say it ain't so, the docs sure do say it is string only…. dang!"

[Update: the string-only restriction was Beta 1, docs to be fixed soon]

The UI looks like this when it comes up:

TagYoureItClean

(I've added 4 numbers in blue along the right hand side so that I can refer to areas of the screen)

Digression: I really liked Algerian as a font for my drop down but it turns out that it is not a native font for Silverlight, so I had to "embed it" in the application. Blend makes this quite easy to do and it is documented in the Blend User Guide (Help file) – but be sure to read the section that applies to Silverlight 2. Briefly you bring up the Font Manager, check the font you want (in my case, Algerian), check the subset you want (I chose All glyphs) and click OK.

When you return to Visual Studio, you'll find a folder in the project named Fonts with your font in it. Very nice.

QuiteFontOfIt

That makes the drop down list go from this

BoringButton

to this

InterestingButton

 

 

(which may or may not be an improvement)

</digression>

How the Tag works

Tagging is pretty straight forward; you just assign an object to the control's tag, and you fish it out later, remembering that the Tag is of type object and so you must cast back to the "real" (e.g., runtime) type of whatever you've stored.

Setting up a test that will actually mean something is not always trivial.

Our test form works like this: you choose to check or not, the check box, to pick a radio button and to choose one of the Prog-Metal bands in the drop down.  You also fill in the fields that we'll use to create a new Person object. The Person object is defined in Person.cs like this:

   1: public class Person
   2: {
   3:    public string Name { get; set; }
   4:    public int Age { get; set; }
   5:    public string Email { get; set; }
   6:    public string Skype { get; set; }
   7:    public Person( string name, int age, string email, string skype 
   8:    {
   9:       Name = name;
  10:       Age = age;
  11:       Email = email;
  12:       Skype = skype;
  13:    }
  14: }

When you push the pushy button the first time, I gather the tags (if they are not null) and I disable all the controls except the button. When you push it the second time, I report what is in the tags using the Message text box. Being a Quick and Dirty program, if you don't touch a control, I don't set the tag, so be cooperative and touch all the controls before pressing Pushy.  A typical run looks like this:

– Check Wicked Cool (you are wicked cool, right?)

– Check Thing Two (I always liked Thing Two better)

– Drop the drop down and pick the group you like. For some reason, I find the arrow keys work best)

– Fill in the four fields, or at least all but Skype

– Press Pushy

– Press Pushy again

– Send me email with all the ways I should have improved this demo (which of course I will when I make this a video). 

You'll actually get the most out of this if you step through the code (which is here).

The key is that the second push of the Pushy button extracts all the tags, including the tag that has the Person object.  I have a note in to the developers asking if I'm misleading you showing that this seems to work. I'll get back to you, but it does seem to work.

I'm lying about my age, I'm 34. I swear.

Yes, But What Are They For?

Just before posting I received a wonderful email from Austin Avrashow who shared this anecdote from his blog

once asked a lady with an English accent working behind a sandwich counter about an ingredient of one of the sandwiches. I've heard of chutney, I told her, but what can you use it on?
She brightened up and said, "Everything!" I told her that didn't really help me

Right! I've told you that it works, and how to use it, but not what it is for.  And that, of course, is the failing in so many technical articles.  The answer is… you use it when you need to stash away information that will help you make a decision later but you have no place to put it.

Here's an example.  In my forthcoming book, I want to be able to iterate through all the objects on a page and tell which ones are my controls and which are not.

I'll be adding controls and removing controls, and there will be layout controls and all sorts of controls in the page.   There are a lot of ways to do this, but one quick and easy way is for me to put what amounts to a cookie into each of my control's Tag properties  that says "I'm what you're looking for." 

I can then pull out each control from the Children collection of the page and if they have the cookie, I know they're mine and I can take the appropriate action.

Very nice, very easy, and no need for a derived class of "ControlsICareAbout."

More soon.

Thanks.

-j

Housekeeping Notes

Quick note to readers:  While I will be able to post (nearly) every day, the 1pm deadline is not going to happen. So let's make it 5pm Eastern and I'll be early rather than late.

Second, I've received quite a bit of "where is that advanced material? email.  My response is "Yup, you're right!" With RC0 and RTW on the way I can't guarantee it will be immediate, but I can tell  you it is a priority (as is a steady stream of introductory material). 

Thanks again.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.