c# - 将 BasedOn 属性与在不同字典中定义的样式一起使用

标签 c# silverlight resourcedictionary mergeddictionaries

我正在处理的应用程序有 2 个 ResourceDictionary、DefaultStyles.xaml 和 CustomStyles.xaml。

CustomStyles 字典中的样式是否可能使用其他字典中定义的基本样式?

DefaultStyles.xaml:

<Style x:Key="TextBlockDefaultStyle" TargetType="TextBlock">  
    <Setter Property="Margin" Value="4" />  
</Style>  

CustomStyles.xaml:

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}">
    <Setter Property="FontSize" Value="16" />
</Style>

App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles/DefaultStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/CustomStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

代码运行时,抛出如下异常:

找不到名称/键为 TextBlockDefaultStyle 的资源。

如果两种样式都在同一个文件中,效果会很好。

最佳答案

您需要直接引用其他样式的字典。

CustomStyles.xaml:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="DefaultStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}">
    <Setter Property="FontSize" Value="16" />
</Style>

关于c# - 将 BasedOn 属性与在不同字典中定义的样式一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649626/

相关文章:

c# - 如何使用 model.id 动态生成 HTML 属性值?

c# - 使用异步和事件访问 UI 线程上的 UI 控件

c# - 使用 ScrollViewer 单击并拖动滚动

wpf - ResourceDictionary 更改如何导致 DynamicResource 重新评估?

wpf - 蒙皮:使用一种颜色作为另一种颜色的静态资源

.net - 无法在 app.xaml 中找到 MergedDictionaries

c# - 使用语音命令启用和禁用语音识别

silverlight - 如何在我的 WP7 应用程序中获取当前页面

c# - 重绘 Silverlight Canvas

c# - silverlight/wpf 中的交点