I came across some weird behavior referring to StaticResources from the DataTemplate defined in ResourceDictionary.
In this example, I populate the list with numbers 1 through 9 using the DataTemplate defined in ResourceDictionary.
Here is the MainWindow.xaml code:
<Window x:Class="testResources.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="525" Height="350"> <Grid> <ListBox Width="100" ItemTemplate="{StaticResource NumberTemplate}"> <ListBox.ItemsSource> <Int32Collection>1,2,3,4,5,6,7,8,9</Int32Collection> </ListBox.ItemsSource> </ListBox> </Grid>
NumberTemplate defined in ResourceDictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <DataTemplate x:Key="NumberTemplate"> <Grid Background="{StaticResource CoolNumbersColor}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="35" /> </Grid.ColumnDefinitions> <TextBox Grid.Column="0" Background="{StaticResource CoolNumbersColor}" Text="{Binding Mode=OneWay}" /> </Grid> </DataTemplate>
StaticResource CoolNumbersColor defined in App.xaml along with ResourceDictionary1.xaml . Here is my App.xaml file:
<Application x:Class="testResources.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <SolidColorBrush x:Key="CoolNumbersColor">GreenYellow</SolidColorBrush> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/ResourceDictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
First of all, I see the expected behavior in the designer of Visual Studio 2010. Indeed, a colored list of numbers appears. But when I try to run this sample, I get an error
"Cannot find resource named CoolNumbersColor. Case sensitive"
I do not understand why this is happening. CoolNumbersColor score CoolNumbersColor delayed somehow? Lexically, it is in front of a unified resource.
The only way to make this work (other than using DynamicResources) is to create a second ResourceDictionary (for example, ResourceDictionary2.xaml), define CoolNumbersColor there and merge them all into ResourceDictionary.MergedDictionaries as follows:
<Application x:Class="testResources.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/ResourceDictionary2.xaml" /> <ResourceDictionary Source="pack://application:,,,/ResourceDictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>