简体   繁体   English

带有ListBox的WPF DataTemplate

[英]WPF DataTemplate with ListBox

I have a very complex DataTemplate which contains three listbox, where they all have a data template for their own and a style for grouping 我有一个非常复杂的DataTemplate,它包含三个列表框,它们都有自己的数据模板和分组样式

I'm getting an exception: {"Cannot find resource named 'ContainerStyle'. Resource names are case sensitive."} 我得到一个例外:{“找不到名为'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>

StaticResource requires that the referenced resource name be in scope ahead of the usage at load time. StaticResource要求引用的资源名称在加载时的使用之前在范围内。 Can't tell here but if you have ContainerStyle declared later in the file than your templates that would cause this error. 这里不能告诉你,但如果你在文件中稍后声明的ContainerStyle比导致此错误的模板。 If not there are a lot of other possible ways this could be setup that would cause it to not be in scope yet, in which case you could try using DynamicResource instead. 如果没有,可以设置很多其他可能的方法,这将导致它不在范围内,在这种情况下,您可以尝试使用DynamicResource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM