Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit theme error:The property 'Content' was not found in type 'System.Windows.Controls.Control'

I have got an error while trying to upgrade our large project to SL4. I didn't write the original theme and my theme knowlege isn't great. In my demo app I have a Label and a LabelHeader(which i have created and is just a derived class from Label with DefaultStyleKey = typeof(LabelHeader);
I am styling the LabelHeader like this:

 <Style TargetType="themeControls:LabelHeader">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <DataInput:Label 
                    FontSize="{TemplateBinding FontSize}" 
                    FontFamily="{TemplateBinding FontFamily}" 
                    Foreground="{TemplateBinding Foreground}" 
                    Content="{TemplateBinding  Content}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="20"/>
    <Setter Property="Foreground" Value="Red"/>
</Style>

This works in SL3 but in SL4 I get:

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

If I change this: Content="{TemplateBinding Content}" to Content="XXX" Then there is no error but , of course, I get XXX in my label rather than the content I set in XAML on the page

Any ideas how I can get this working?

Demo project here:

http://walkersretreat.co.nz/files/ThemeIssue.zip

(Apologies for reposting, I have so far got no answers over here: http://forums.silverlight.net/forums/p/183380/415930.aspx#415930)

EDIT The answer provided looks like it will work. An issue has been opened for this issue here: https://connect.microsoft.com/VisualStudio/feedback/details/561183

vote if you think this is important!

like image 708
Mark Avatar asked May 24 '10 01:05

Mark


2 Answers

Thanks to Wolf Schmidt (MSFT)'s posting on the 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".

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.

Sourced from: - our forum discussions on Silverlight .NET: (not enough reputation to post >1 hyperlink) - my the MS Connect issue: https://connect.microsoft.com/VisualStudio/feedback/details/561183

like image 146
Jaans Avatar answered Nov 20 '22 14:11

Jaans


There's a bug in some situations in Silverlight 4 when this exception arises. It's specific to the Content Property.

The temporary fix seems to be to use a normal binding for the Content property.

Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"

Though I haven't tested this solution out yet.

like image 4
JustinAngel Avatar answered Nov 20 '22 13:11

JustinAngel