Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分组后,带有模板的WPF列表框项将不可见_Wpf_Templates_Listbox_Grouping_Expander - Fatal编程技术网

分组后,带有模板的WPF列表框项将不可见

分组后,带有模板的WPF列表框项将不可见,wpf,templates,listbox,grouping,expander,Wpf,Templates,Listbox,Grouping,Expander,我有一个ObservableCollection,其中包含我想在列表框中显示的项目。 我还为ListboxItem编写了一个模板,以正确显示我的收藏。 在这个阶段,一切正常 in.cs Sensors = new ObservableCollection<Sensor>(); ... lstBox.ItemsSource = Sensors; in.xaml ... ... ... 此代码可以工作并对项目进行分组,但项目将不可见。 因此,如果没有正确显示分组项目,但使用分组

我有一个
ObservableCollection
,其中包含我想在
列表框中显示的项目。
我还为
ListboxItem
编写了一个模板,以正确显示我的收藏。 在这个阶段,一切正常

in.cs

Sensors = new ObservableCollection<Sensor>();
...
lstBox.ItemsSource = Sensors;  
in.xaml


...
...
...
此代码可以工作并对项目进行分组,但项目将不可见。
因此,如果没有正确显示分组项目,但使用分组扩展器,则不会显示任何内容。

我认为
扩展器中的
itemsresenter
有些问题,但我不知道是什么问题。

问题在于我在应用程序中使用的一个第三方主题。该主题有一个
列表框
模板,如:

<Style TargetType="{x:Type ListBox}">
...
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Grid>
                        <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Background="{DynamicResource ControlBackgroundBrush}" />
                        <ScrollViewer Margin="1" Style="{DynamicResource NuclearScrollViewer}" Focusable="false" Background="{x:Null}">
                            <StackPanel Margin="1,1,1,1" IsItemsHost="true" />
                        </ScrollViewer>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border" />
                            <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border" />
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

...
因此,我在该模板中使用
ItemsPresenter
而不是
StackPanel
,现在一切正常

...
ICollectionView view = CollectionViewSource.GetDefaultView(Sensors);
view.GroupDescriptions.Add(new PropertyGroupDescription("GroupNumber"));

lstBox.ItemsSource = view;
...
<!--Same Template and Style-->
...
...
<Style x:Key="GroupContainerStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">        
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Group #" />
                                    <TextBlock Text="{Binding Name}" />
                                </StackPanel>
                            </Expander.Header>
                            <Expander.Content>
                                <ItemsPresenter />
                            </Expander.Content>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
...
<ListBox Name="lstBox" Focusable="False" 
                             SelectionChanged="lstBox_SelectionChanged" 
                             HorizontalContentAlignment="Stretch" 
                             ItemTemplate="{StaticResource SensorTileTemplate}"
                             ItemContainerStyle="{StaticResource ContainerStyle}">
                    <ListBox.GroupStyle>
                        <GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}" />
                    </ListBox.GroupStyle>
                </ListBox>
<Style TargetType="{x:Type ListBox}">
...
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Grid>
                        <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Background="{DynamicResource ControlBackgroundBrush}" />
                        <ScrollViewer Margin="1" Style="{DynamicResource NuclearScrollViewer}" Focusable="false" Background="{x:Null}">
                            <StackPanel Margin="1,1,1,1" IsItemsHost="true" />
                        </ScrollViewer>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border" />
                            <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border" />
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>