سؤال

I have ObservableCollection with items which I want to display in a ListBox. Also I write a template for ListboxItem for correct display of my collection. On this stage everything works fine.

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>

The problem appears when I need to group certain items using expander as a group container.

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>

This code works and groups items but items become invisible.
So without grouping items display correctly but with grouping expanders show nothing in it.
I think there is something about ItemsPresenter in Expander but can't figure out what.

هل كانت مفيدة؟

المحلول

The problem was in one third party theme I use in my app. That theme has a ListBox template like:

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

So I use an ItemsPresenter instead of the StackPanel in that template and everything works now.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top