In a previous post, I’ve introduced the concept of content properties. The idea behind a content property is simple: you use the ContentPropertyAttribute to specify the property which will be set with the contents declared inside that element when the property element syntax is not used. Here’s a quick example that should make this concept perfectly clear:
<Button x:Name="info"> <TextBlock Text="Say hi!" /> </Button>
In the previous snippet, the info Button’s Content property will be set to the TextBlock specified inside the <Button> element (btw, that happens because Button ends up inheriting from ContentControl which is annotated with the ContentPropertyAttribute to indicate that inner XAML contents are supposed to be passed to the Content property).
I’m only mentioning this topic again because there’s (another) small problem you might encounter if you’re coming into Silverlight from the WPF world. Take a look at the following snippet:
<Button x:Name="info"> Say hi </Button>
Loading that XAML means getting this error:
Ok, so what’s going on here? Once again all boils down to the XAML parser which is used by Silverlight. The following doc says this:
With the exceptions of TextBlock and Run, object elements in Silverlight cannot contain XML text nodes as an implicit way to set their text-type content properties. For example, <Button> hello world </Button> is not allowed in Silverlight XAML, you would have to specify <Button Content="hello world"…/>. Again, note that there are some syntaxes (such as for Color) that might resemble object element with inner text syntax, but they are technically an initialization text syntax, which supplies the string to type-convert for initialization as inner text.
Not something I enjoy, but that’s the way it works. And that’s it for now. Stay tuned for more.