I defined some StaticResource styles inside App.xaml to use it across the app:
<Application x:Class="TaskListApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TaskListApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="primaryColor" Color="#B5B4D9"/>
<SolidColorBrush x:Key="secondaryColor" Color="#393E59"/>
<SolidColorBrush x:Key="backgroundColor" Color="#2a2e42"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
<ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
<ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
<ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Using it in MainWindow.xaml is making no errors and works just fine:
<Grid Grid.Row="2"
Background="{StaticResource backgroundColor}"
</Grid>
But when I added it inside a ResourceDictionary (in this case TaskListTheme.xaml), I got an exception:
Exception: Cannot find resource named 'primaryColor'. Resource names are case sensitive.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="ListBoxItem" x:Key="TaskListTheme">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Label Grid.Column="1" Content="{Binding TaskListItemName}"
Margin="0"
Height="50"
Background="{StaticResource primaryColor}"
Padding="10,0,0,0"
Foreground="{StaticResource secondaryColor}"
FontSize="15"
FontWeight="SemiBold"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
BorderThickness="0"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
How can I resolve this kind of problem?
CodePudding user response:
If a resource in TaskListTheme.xaml has a dependency on a resource that is defined in another resource dictionary, you should merge the latter into TaskListTheme.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
So move your *Color brush resources to Colors.xaml and then either merge Colors.xaml into the other resource dictionaries or into App.xaml
:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"/>
<ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
<ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
<ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
<ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
CodePudding user response:
I think mm8's answer is standard way. Said that, you can just move resources directly under Application.Resources to ResourceDictionary inside MergedDictionaries to achieve the same effect.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="primaryColor" Color="#B5B4D9"/>
<SolidColorBrush x:Key="secondaryColor" Color="#393E59"/>
<SolidColorBrush x:Key="backgroundColor" Color="#2a2e42"/>
</ResourceDictionary>
<ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
<ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
<ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
<ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>