ResourceDictionary에 정의 된 DataTemplate 내부에서 StaticResources를 참조 할 때 이상한 동작이 발생했습니다.DataTemplate 내의 StaticResource 참조
이 예제에서는 ResourceDictionary에 정의 된 DataTemplate을 사용하여 1에서 9까지의 숫자로 목록 상자를 채 웁니다.
<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
이 ResourceDictionary1.xaml에 정의되어 있습니다 :
는 여기 MainWindow.xaml 코드 년대 정적 리소스 CoolNumbersColor
이 응용 프로그램에 정의되어
<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>
.과 함께 xaml. 난 비주얼 스튜디오 2010 디자이너에서 예상되는 동작을 볼 수 있습니다 모든
이
<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>
첫째, 여기 내 App.xaml 파일입니다. 실제로 색칠 된 숫자 목록이 나타납니다. 이 샘플을 실행하려고 할 때 나는 오류를받을
" 'CoolNumbersColor'라는 이름 리소스를 찾을 수 없습니다. 자원 이름은 대소 문자를 구분"
이런 일이 발생하는 이유는 이해할 수 없다. CoolNumbersColor
평가가 어떻게 되던가? 어휘 적으로 병합 된 resourcedictionary 앞에 있습니다.
이 작업을 수행하는 유일한 방법은 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>
이 리소스를 어디에서 사용하십니까? 나는 작은 응용 프로그램에서 그것을 시도하고 내 끝에서 잘 작동합니다. –
Application.Resources의 ResourceDictionary.MergedDictionaries 앞에 CoolNumbersColor를 정의하려고 했습니까? –
예'ListBox'의'DataTemplate' 안에서 사용하고 사용했습니다. 여전히 문제는 없습니다. 작은 응용 프로그램으로 시도해 볼 수 있습니까? 나는 다른 곳에서 문제가 있다고 생각한다. –