Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39998

Using style in StandardStyle xaml from another Style xaml

I have a custom Color.xaml as

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="MyColor1" Color="#7d897d"/>
    <SolidColorBrush x:Key="MyColor2" Color="#078ab4"/>
</ResourceDictionary>

And App.xaml as

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Defines the colors used in the app-->
                <ResourceDictionary Source="Color.xaml"/>
                <!-- Styles that define common aspects of the platform look and
                     feel Required by Visual Studio project and item templates-->
                <ResourceDictionary Source="StandardStyles.xaml"/>
                <ResourceDictionary>
                ............

In My StandardStyle.xaml I'm not able to use the Color defined in the xaml.

<Style x:Key="HeadingTextStyle" TargetType="TextBlock">
    <Setter Property="FontWeight" Value="SemiLight"/>
    <Setter Property="FontSize" Value="20"/>
    <Setter Property="Foreground" Value="{StaticResource MyColor1}"/>
</Style>

It gives me an exception when I run the code

"Cannot find a Resource with the Name/Key MyColor1 [Line: 20 Position: 44]"

However I'm able to use this color in the UI xaml files.

Upvotes: 1

Views: 870

Answers (1)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39998

Here it is, include the Color.xaml in StandardStyle.xaml as

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

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

    <!-- your styles here -->

</ResourceDictionary>

Upvotes: 1

Related Questions