Home Dashboard Directory Help
Search

Silverlight 4 Bug - TemplateBinding for Content property on style for label control fails by Jaans


Status: 

Closed
 as By Design Help for as By Design


4
0
Sign in
to vote
Type: Bug
ID: 561183
Opened: 5/24/2010 3:55:53 PM
Access Restriction: Public
1
Workaround(s)
view
0
User(s) can reproduce this bug

Description

A style definition that worked fine in Silverlight 3 now results in the following error in Silverlight 4:

Error: Unhandled Error in Silverlight Application
Code: 2500    
Category: ParserError    
Message: The property 'Content' was not found in type 'System.Windows.Controls.Control'.

Here is the style definition:
    <Style x:Key="LabelHeader" TargetType="controls:Label">
            <Setter Property="Margin" Value="0" />
            <Setter Property="Opacity" Value=".6" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="#CCCCCCCC" BorderThickness=".5" Background="#CCEFEFEF">
                            <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2" Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


And here is the usage of the style:
<controls:Label Style="{StaticResource LabelHeader}" Content="{Binding SiteHeader, Source={StaticResource Localization}}" />

By accident the following seems to temporarily fix the issue as a work around. The following is a different definition for the content presenter section of the style above:

<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />

The whole point of template bindings are to avoid the above labour intensive style templates.
Details
Sign in to post a comment.
Posted by Jaans on 5/26/2010 at 6:02 PM
Thanks to Wolf Schmidt (MSFT)'s posting on the http://www.silverlight.net forum, the issue has been identified, explained and potentially resolved as designed. According to him, Silverlight 4 will now consider Silverlight 3's quasi-dynamic behaviour with regard to ControlTemplates in a stricter way, which results in an error in Silverlight 4 when the property does not exist for the TargetType of the ControlTemplate. What's important to note is that when the TargetType is not specified on the ControlTemplate it defaults to TargetType="Control".

Here is the MSDN documentation (albeit somewhat hidden):
http://msdn.microsoft.com/en-us/library/ff457753(VS.95).aspx
(Scroll down to the template behaviour bullets, specifically the 2nd last one).

Here is the Silverlight.NET forum thread where Wolf Schmidt (MSFT) posted his explanation:
http://forums.silverlight.net/forums/p/183380/419535.aspx#419535

The resolution to the issue is to specify a TargetType for the ControlTemplate such that the properties used in the template binding will resolve.

Here's an updated style with the TargetType specified for the ControlTemplate:
    <Style x:Key="LabelHeader" TargetType="controls:Label">
            <Setter Property="Margin" Value="0" />
            <Setter Property="Opacity" Value=".6" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:Label">
                        <Border BorderBrush="#CCCCCCCC" BorderThickness=".5" Background="#CCEFEFEF">
                            <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2" Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

It would however be very usefull to developers if the IDE/intellisense could verify that the property specified in the TemplateBinding actually exists on the defaulted/specified TargetType of the ControlTemplate.
Posted by Microsoft on 5/24/2010 at 9:47 PM
Thanks for your feedback. We were able to reproduce the issue you are seeing. We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 5/24/2010 at 5:06 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Jaans on 5/26/2010 at 6:04 PM
Just for completeness sake, here is the "workaround" / correct style implementation for Silverlight 4.

Specify a TargetType for the ControlTemplate such that the properties used in the template binding will resolve.
Here's an updated style with the TargetType specified for the ControlTemplate:

    <Style x:Key="LabelHeader" TargetType="controls:Label">
            <Setter Property="Margin" Value="0" />
            <Setter Property="Opacity" Value=".6" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:Label">
                        <Border BorderBrush="#CCCCCCCC" BorderThickness=".5" Background="#CCEFEFEF">
                            <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2" Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

It would however be very usefull to developers if the IDE/intellisense could verify that the property specified in the TemplateBinding actually exists on the defaulted/specified TargetType of the ControlTemplate.