我有一个非常复杂的DataTemplate,它包含三个列表框,它们都有自己的数据模板和分组样式
我得到了一个例外: {"找不到名为' ContainerStyle'的资源。资源名称区分大小写。"}
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Merge in the resource dictionary that is shared between the main window and the overview window.
-->
<ResourceDictionary
Source="SharedVisualTemplates.xaml"
/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="MessagesDataTemplate"
DataType="ca:Message">
<Grid>
<TextBlock Text="{Binding Path=Name}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="IncludesDataTemplate"
DataType="ca:Include">
<Grid>
<TextBlock Text="{Binding Path=Name}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DefinitionsDataTemplate"
DataType="ca:Definition">
<Grid>
<TextBlock Text="{Binding Path=Name}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="InterfacesDataTemplate"
DataType="ca:Interface">
<Grid>
<TextBlock Text="{Binding Path=Name}" MouseLeftButtonDown="interface_mouseDown">
<TextBlock.ToolTip>
<ToolTip Focusable="True">
<StackPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" FontSize="16" TextWrapping="Wrap" Width="400"/>
<TextBlock Text="{Binding Path=Description}" FontSize="14" TextWrapping="Wrap" Width="400"/>
<TextBlock TextWrapping="Wrap" Width="400">
<Run Text="IsMulticast: " FontSize="14" />
<Run Text="{Binding IsMultiCast}" FontSize="14"/>
</TextBlock>
</StackPanel>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
<StackPanel>
<ListBox Grid.ColumnSpan="3" Grid.Row="4"
Name="lbMessages" Background="Transparent" ItemsSource="{Binding MessagesView}" ItemTemplate="{StaticResource MessagesDataTemplate}" BorderBrush="Transparent">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
</ListBox>
<ListBox Grid.ColumnSpan="3" Grid.Row="4"
Name="lbIncludes" Background="Transparent" ItemsSource="{Binding IncludesView}" ItemTemplate="{StaticResource IncludesDataTemplate}" BorderBrush="Transparent">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
</ListBox>
<ListBox Grid.ColumnSpan="3" Grid.Row="4"
Name="lbDefinitions" Background="Transparent" ItemsSource="{Binding DefinitionsView}" ItemTemplate="{StaticResource DefinitionsDataTemplate}" BorderBrush="Transparent">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
</ListBox>
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="EnumsDataTemplate"
DataType="ca:Enum">
<Grid>
<TextBlock Text="{Binding Path=Name}" MouseLeftButtonDown="enum_mouseDown"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="StructsDataTemplate"
DataType="ca:Struct">
<Grid>
<TextBlock Text="{Binding Path=Name}" MouseLeftButtonDown="struct_mouseDown"/>
</Grid>
</DataTemplate>
<!-- UI commands. -->
<RoutedUICommand x:Key="Commands.DeleteSelectedNodes" />
<RoutedUICommand x:Key="Commands.CreateNode" />
<RoutedUICommand x:Key="Commands.DeleteNode" />
<RoutedUICommand x:Key="Commands.DeleteConnection" />
<RoutedUICommand x:Key="Commands.ZoomOut" />
<RoutedUICommand x:Key="Commands.ZoomIn" />
<RoutedUICommand x:Key="Commands.JumpBackToPrevZoom" />
<RoutedUICommand x:Key="Commands.FitContent" />
<RoutedUICommand x:Key="Commands.Fill" />
<RoutedUICommand x:Key="Commands.OneHundredPercent" />
<!--
This converts from a scale value to a percentage value.
It is used to convert the value of 'ContentScale' to the percentage zoom level that is displayed in the UI.
-->
<con:ScaleToPercentConverter
x:Key="scaleToPercentConverter"
/>
<!--
Define the visual style for a 'ConnectorItem'.
-->
<Style
TargetType="{x:Type NetworkUI:ConnectorItem}"
>
<!--
Data-binding for the connector hotspot.
ConnectorItem automatically computes its center points and assings this value
to the 'Hotspot' property. This data-binding then 'pushes' the value into the application
view-model.
-->
<Setter
Property="Hotspot"
Value="{Binding Hotspot, Mode=OneWayToSource}"
/>
<!-- The visual template. -->
<Setter
Property="Template"
>
<Setter.Value>
<ControlTemplate
TargetType="{x:Type NetworkUI:ConnectorItem}"
>
<!-- The visual for the connector. -->
<Ellipse
Stroke="{StaticResource nodeBorderBrush}"
Fill="{StaticResource connectorBackgroundBrush}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Style For MainListBox-->
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}" IsExpanded="False">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:2)
StaticResource要求引用的资源名称在加载时的使用之前在范围内。不能告诉这里,但是如果你在文件中稍后声明的ContainerStyle比导致此错误的模板。如果没有,可以设置很多其他可能的方法,这会导致它不在范围内,在这种情况下,您可以尝试使用DynamicResource。