C# 带ListBox的WPF数据模板

C# 带ListBox的WPF数据模板,c#,wpf,exception,listbox,C#,Wpf,Exception,Listbox,我有一个非常复杂的DataTemplate,其中包含三个listbox,它们都有自己的数据模板和分组样式 我得到一个例外: {“找不到名为“ContainerStyle”的资源。资源名称区分大小写。“} StaticResource要求在加载时引用的资源名称在使用之前在范围内。此处无法确定,但如果您在文件中声明的ContainerStyle比您的模板晚,则会导致此错误。如果没有,有很多其他可能的方法可以设置它,这将导致它还不在范围内,在这种情况下,您可以尝试使用DynamicSource来代替

我有一个非常复杂的DataTemplate,其中包含三个listbox,它们都有自己的数据模板和分组样式

我得到一个例外: {“找不到名为“ContainerStyle”的资源。资源名称区分大小写。“}



StaticResource要求在加载时引用的资源名称在使用之前在范围内。此处无法确定,但如果您在文件中声明的ContainerStyle比您的模板晚,则会导致此错误。如果没有,有很多其他可能的方法可以设置它,这将导致它还不在范围内,在这种情况下,您可以尝试使用DynamicSource来代替。

声明的
容器样式在哪里?它应该位于
StaticResource
扩展可以访问的位置。我现在将编辑消息,以便您可以看到整个代码在DataTemplate之后声明ContainerStyle资源,我将使用整个代码编辑帖子。我尝试了DynamicSource,但它仍然抛出了一个异常。有什么建议吗?@Yogevnn“ContainerStyle”在最后声明了——它必须在所有引用之前声明。@McGarnagle谢谢!就这样
 <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>