【问题标题】:Implicit theme error:The property 'Content' was not found in type 'System.Windows.Controls.Control'隐式主题错误:在类型“System.Windows.Controls.Control”中找不到属性“内容”
【发布时间】:2010-05-24 01:59:02
【问题描述】:

我在尝试将我们的大型项目升级到 SL4 时遇到错误。 我没有写原始主题,我的主题知识也不是很好。 在我的演示应用程序中,我有一个 Label 和一个 LabelHeader(我已经创建了它,它只是 Label 的派生类,带有 DefaultStyleKey = typeof(LabelHeader);
我正在为 LabelHeader 设置样式:

 <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>

这在 SL3 中有效,但在 SL4 中我得到:

错误:Silverlight 应用程序中未处理的错误 代码:2500
类别:ParserError
消息:在“System.Windows.Controls.Control”类型中找不到属性“内容”。
文件:
线路:9
位置:168

如果我改变这个: 内容="{模板绑定内容}" 到 内容="XXX" 然后就没有错误了,但是,当然,我在我的标签中得到了XXX,而不是我在页面上的XAML中设置的内容

有什么想法可以让这个工作吗?

这里是演示项目:

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

(抱歉转帖,我这里暂时没有答案:http://forums.silverlight.net/forums/p/183380/415930.aspx#415930

编辑提供的答案看起来会起作用。此处已针对此问题打开了一个问题: https://connect.microsoft.com/VisualStudio/feedback/details/561183

如果您认为这很重要,请投票!

【问题讨论】:

    标签: silverlight silverlight-4.0 themes


    【解决方案1】:

    感谢 Wolf Schmidt (MSFT) 在 www.silverlight.net 论坛上的发帖,该问题已被识别、解释并可能按设计解决。据他介绍,Silverlight 4 现在将更严格地考虑 Silverlight 3 关于 ControlTemplates 的准动态行为,当 ControlTemplate 的 TargetType 属性不存在时,这会导致 Silverlight 4 中的错误。需要注意的是,当 ControlTemplate 上未指定 TargetType 时,它​​默认为 TargetType="Control"。

    解决该问题的方法是为 ControlTemplate 指定 TargetType,以便模板绑定中使用的属性能够解析。

    以下是为 ControlTemplate 指定 TargetType 的更新样式:

    <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>
    

    但是,如果 IDE/intellisense 可以验证 TemplateBinding 中指定的属性确实存在于 ControlTemplate 的默认/指定 TargetType 上,那么这对开发人员将非常有用。

    来源: - 我们在 Silverlight .NET 上的论坛讨论:(没有足够的声誉来发布 >1 个超链接) - 我的 MS Connect 问题:https://connect.microsoft.com/VisualStudio/feedback/details/561183

    【讨论】:

    【解决方案2】:

    在 Silverlight 4 的某些情况下出现此异常时存在错误。它特定于内容属性。

    临时修复似乎是对 Content 属性使用普通绑定。

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

    虽然我还没有测试过这个解决方案。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2021-06-29
    • 1970-01-01
    • 2019-08-05
    • 2018-11-18
    • 2012-12-10
    相关资源
    最近更新 更多