Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In many scenarios, a property assignment is simple. For example, assigning a number to a Width property is easy to express in a string. But with other assignments it can get more awkward. Why is that? Well, sometimes the item you want to assign to a property is itself an object with its own properties that need to be set. In other words, the property assignment is complex. A good example in Silverlight is when you want to paint the background of an element, but you don't want a simple solid color. Instead, you think that a multi-color gradient or a tiled image would make a nicer background.
This complexity is difficult to express within a string. For a short example, take a look at the XAML on the slide. Note my comment at the top, Bad XAML, do not mimic. This is showing how hard it would be to try to express a complex setting inside a single string, with a starting single quote and ending single quote. To simplify expressing these complex scenarios, Silverlight has the Property Element syntax. These are an alternative to Property Attributes. They can be substituted wherever a Property Attribute is used.
Some tools, especially Expression Blend, will use Property Elements in their generated code. Just to clarify, an Object Element results in the instantiation of a .NET type. A Property Element in XAML does not create an instance. Instead it sets the value of a property. Property Elements are always nested inside an Object Element. And they are distinguished from Object Elements by having a period in the element name. So in my example on this slide, I have TextBlock and I'm setting the Text property on line 9 with a Property Element, TextBlock.Text.
And then in between the beginning and ending tag is the data that I'm assigning to that property. Let me show you that in Visual Studio. I'm inside a solution called PropertyElements and I've opened my MainPage.xaml. Here are two examples of setting the FontSize. This first example is setting it using a Property Attribute and the second example is setting it using a FontSize Property Element. Here I am setting the Fill property of this Ellipse.
The first Ellipse has the Fill set to green, where the second Ellipse is more complicated. This is the Ellipse, instantiate an instance of the Ellipse, and then set the Ellipse.Fill property. Within that Fill property, I'm instantiating an instance of the LinearGradientBrush and assigning that to the Fill. And then I'm going to LinearGradientBrush.GradientStops, which is a collection, and I'm assigning two gradient stops. I can even set properties like this; the GradientStop.Offset and the GradientStop.Color.
So I am setting a LightBlue color for one of the bands of the gradient here. I thought I would also show you in Blend how it generates these Property Elements. I have the same project open inside Expression Blend. And I am going to open this file, AddPropertyInBlend.xaml. Next, I am going to click this button and I'm going to show you the XAML for this button. In Blend you can look at the raw XAML by clicking on this XAML button. Here is the XAML. I have a Button and it has a number of simple properties.
What I am going to do next is switch back to the Design View, select my Button, and then I am going to scroll down in the Property pane on the right side of my window, down to this section called Transform. You may or may not see this. You might have to open it by clicking on this little tab here. And then I am going to go down to the Projection section. This allows me to do a PlaneProjection on the button. So I'll take the X coordinate. In Blend, I can type in a number here, or I can use the special cursor.
Notice how my cursor has four arrowheads on it. What that means is that I can place my mouse over the Y angle value and then I can Hold my left mouse button down, and then I can scroll up, down, left, or right to modify this value. So I am changing the PlaneProjection to look like I'm spinning this button. That's fun. But the point of this demo was to show you the XAML is generated. So back to the XAML View, on Line 17 you can see the Expression Blend generated a Property Element for me.
I encourage you to explore both property syntaxes, as you will encounter them frequently in Silverlight examples.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99197 Viewers
56 Video lessons · 112477 Viewers
71 Video lessons · 81293 Viewers
131 Video lessons · 39036 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.