0

在我的应用程序中,我有一个ContentControl,这显示了Content使用DataTemplates.

现在,我需要将它传递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 的资源

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

4

1 回答 1

0

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

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

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

于 2012-08-24T08:55:24.463 回答