具有模板的WPF ListBox项目在分组后变为不可见

时间:2013-03-21 03:01:05

标签: wpf templates listbox grouping expander

我有ObservableCollection项目,我希望在ListBox中显示这些项目。 我还为ListboxItem编写了一个模板,以便正确显示我的收藏。 在这个阶段,一切正常。

in .cs

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

in .xaml

...
 <DataTemplate x:Key="SensorTileTemplate">
            <Border>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.ColumnSpan="3"></TextBlock>
                    <Image Source="{Binding ImageModel.ImgSource}" Style="{StaticResource ImageGlowStyle}" Height="72" Grid.Row="1" Grid.Column="0"></Image>
                    <StackPanel Grid.Row="1" Grid.Column="1" Margin="5">
                        <TextBlock Text="IP:"></TextBlock>
                        <TextBlock Text="Port:"></TextBlock>
                        <TextBlock Text="Command port:"></TextBlock>
                    </StackPanel>
                    <StackPanel Grid.Row="1" Grid.Column="2" Margin="5">
                        <TextBlock Text="{Binding DeviceAddress}"></TextBlock>
                        <TextBlock Text="{Binding DeviceDataPort}"></TextBlock>
                        <TextBlock Text="{Binding DeviceControlPort}"></TextBlock>
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>

<Style x:Key="ContainerStyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="ListBoxItem.Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

...

<ListBox Name="lstBox" Focusable="False" 
                             SelectionChanged="lstBox_SelectionChanged" 
                             HorizontalContentAlignment="Stretch" 
                             ItemTemplate="{StaticResource SensorTileTemplate}"
                             ItemContainerStyle="{StaticResource ContainerStyle}">
                </ListBox>

当我需要使用扩展器作为组容器对某些项目进行分组时,会出现问题。

in .cs

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

lstBox.ItemsSource = view;
...

in .xaml

<!--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>

此代码可以工作并对项目进行分组,但项目将变为不可见 因此,如果没有正确分组项目,但分组扩展器中没有显示任何内容 我认为ItemsPresenter中有Expander的内容,但无法弄清楚是什么。

1 个答案:

答案 0 :(得分:1)

问题在于我在我的应用中使用的第三方主题。该主题有一个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>

所以我在该模板中使用ItemsPresenter代替StackPanel,现在一切正常。