【问题标题】:How to set a ResourceDictionary to a ContentControl.Content?如何将 ResourceDictionary 设置为 ContentControl.Content?
【发布时间】:2012-08-18 10:33:14
【问题描述】:

在我的应用程序中,我有一个ContentControl,这显示了使用DataTemplatesContent 属性。

现在,我需要将此 DictionaryResource 传递给 content 属性。所以我这样做了(我不确定)

        <ContentControl Content="{Binding CurrentViewModel">
            <ContentControl.Resources>
                <ResourceDictionary Source="/MathematicsBusiness.Infrastructure;component/Resources/ThemeResources.xaml" />
            </ContentControl.Resources>
        </ContentControl>

这包含我的字典:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="40" />
        <Setter Property="FontFamily" Value="Georgia" />
    </Style>

    <!--<Style x:Key="TextBlockStyle" TargetType="TextBlock">
        <Setter Property="FontSize" Value="40" />
        <Setter Property="FontFamily" Value="Georgia" />
    </Style>-->
</ResourceDictionary>

而且它有效,所有数据模板都显示具有该样式的文本块。但是如果我使用注释样式,它就不起作用。它给我一个错误:

找不到具有名称/键 TextBlockStyle 的资源

为什么会这样?如果样式没有键,它可以工作。但是如果我设置一个键,它就不起作用了。

【问题讨论】:

  • 您能否举例说明您是如何使用TextBlockStyle 键的?

标签: silverlight xaml styles silverlight-5.0 resourcedictionary


【解决方案1】:

如果您指定一个密钥而不使用它,这应该不是问题。我的猜测是您在其他地方调用 TextBlockStyle(可能是您的视觉状态或您的代码)。

如果您尝试使用密钥,但您没有在 xaml 中指定它,通常会发生错误。

其他可能的原因是您尝试使用 ContentControl 之外的密钥。

【讨论】:

    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2012-05-22
    • 1970-01-01
    相关资源
    最近更新 更多