WPF:由于Style和/或修改后的ListBox,Groupstyle无法正常工作

时间:2011-06-01 16:21:29

标签: wpf listbox groupstyle

我正在尝试使用GroupStyle,但不是将组名显示为列表中项目的标题,而是仅显示标题而不显示项目。

在我将特殊样式应用于列表之前,它工作正常。然后我决定这种风格很假,所以我为这个效果创建了一个UserControl。问题依然存在。

UserControl的目的是对所选项目产生扩展效果,通常它可以显示一些信息,然后在展开时显示更多信息。

用户控件:

<UserControl x:Class="MyProject.CustomUC.ExpandingList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <ListBox x:Name="List"
                 ItemsSource="{Binding Path=Collection}">
            <ListBox.Template>
                <ControlTemplate TargetType="ListBox">
                    <Border BorderBrush="Blue" 
                                    BorderThickness="1" 
                                    CornerRadius="2" 
                                    Background="White">
                        <ScrollViewer Focusable="False">
                            <StackPanel Margin="2" IsItemsHost="True" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.GroupStyle>
                <GroupStyle HidesIfEmpty="True">
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border Margin="4" Name="Border" BorderBrush="Blue" BorderThickness="1" CornerRadius="5" Background="LightBlue">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <ContentPresenter Name="Head"
                                                          Margin="4"
                                                      Visibility="Visible"
                                                      ContentTemplate="{Binding ElementName=List, Path=DataContext.HeadTemplate}"/>
                                        <Border Name="Tail" Visibility="Collapsed" Grid.Row="1" BorderThickness="0,1,0,0" BorderBrush="Blue">
                                        <ContentPresenter Margin="4"
                                                      ContentTemplate="{Binding ElementName=List, Path=DataContext.TailTemplate}" />
                                        </Border>
                                    </Grid>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Tail" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</UserControl>

使用UserControl:

<CustomUC:ExpandingList Collection="{Binding Path=List}">
    <CustomUC:ExpandingList.HeadTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}" />
        </DataTemplate>
    </CustomUC:ExpandingList.HeadTemplate>
    <CustomUC:ExpandingList.TailTemplate>
        <DataTemplate DataType="{x:Type ViewModel:ElementViewModel}">
            <TextBlock Text="{Binding Path=SomeOtherProperties}" />
        </DataTemplate>
    </CustomUC:ExpandingList.TailTemplate>
</CustomUC:ExpandingList>

如果我从ExpandingList UserCotnrol:

更改,则此方法有效
<ListView ItemsSource="{Binding Path=List}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}" />
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.GroupStyle>
        <GroupStyle HidesIfEmpty="True">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

任何人都知道可能出现什么问题?

1 个答案:

答案 0 :(得分:4)

ListBox模板中,替换:

<StackPanel Margin="2" IsItemsHost="True" />

<ItemsPresenter Margin="2" />

ItemsPresenter类为组提供额外的逻辑,如果您直接使用面板显示项目,则会丢失。