У меня возникло странное поведение при обращении к StaticResources изнутри DataTemplate, определенного в ResourceDictionary.
В этом примере я заполняю список числами от 1 до 9, используя DataTemplate, определенный в ResourceDictionary.
Вот код MainWindow.xaml:
<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>
The NumberTemplate
is 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>
The StaticResource CoolNumbersColor
is defined in App.xaml along with ResourceDictionary1.xaml
. Here's 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 can see the expected behaviour in Visual Studio 2010 designer. Indeed a colored list of numbers appears. But when trying to run this sample I receive the error
«Не удается найти ресурс с именем 'CoolNumbersColor'. Имена ресурсов чувствительны к регистру»
Я не могу понять, почему это происходит. CoolNumbersColor
оценка как-то откладывается? Лексически он находится перед объединенным ресурсным словарем.
Единственный способ выполнить эту работу (кроме использования DynamicResources) - создать второй ResourceDictionary (например, ResourceDictionary2.xaml), определить там CoolNumbersColor
и объединить их все в ResourceDictionary.MergedDictionaries
следующим образом:
<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>
DataTemplate
изListBox
. По-прежнему нет проблем. Можете попробовать в небольшом приложении? Я подозреваю, что проблема в другом. - person Rohit Vats   schedule 07.03.2014