为其中包含 gridview 的 Listview 设置样式

Styling a Listview with a gridview in it

我试图设计我的列表视图模板的样式,其中包含一个网格视图。我尝试使用 ItemsPresenter 来完成它并且它工作正常但是我的 gridview 的 header 消失了。

我应该用什么来保存我的 header?

<Style TargetType="{x:Type ListView}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <Border x:Name="Bd"
                            SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

编辑:我的列表视图

<ListView Name="findReplaceView" Margin="10" Grid.Row="1" Grid.Column="0" 
                  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                  ItemsSource="{Binding FindAndReplaceItems}">
  <ListView.View>
    <GridView AllowsColumnReorder="False">
      <GridViewColumn Header="Find" DisplayMemberBinding="{Binding Find}"
                                    Width="{Binding ElementName=helperField1, Path=ActualWidth}"/>
      <GridViewColumn Header="Replace" DisplayMemberBinding="{Binding Replace}"
                                    Width="{Binding ElementName=helperField2, Path=ActualWidth}"/>
    </GridView>
  </ListView.View>
</ListView>

这是因为您没有设置 header 部分的样式。你应该这样做:

<Style TargetType="{x:Type ListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid HorizontalAlignment="Left">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <!-- This is how your headers are presented -->
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView},Path=View.Columns}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="1" BorderBrush="LightGray" Background="MidnightBlue">
                                        <TextBlock Foreground="WhiteSmoke" 
                                                   TextAlignment="Center"
                                                   FontWeight="Bold" 
                                                   Text="{Binding Header}"
                                                   Width="{Binding ActualWidth}"/>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    <!-- This is how your items are presented -->
                    <Border x:Name="Bd" Grid.Row="1"
                    SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在上面的代码中有2个Grid.RowDefinitionsheight="Auto"height="*"第一个是代表header部分的区域(即顶行)第二个是包含您的 ItemsPresenter 的剩余区域,它决定了您的物品的呈现方式。


编辑

因为这被认为是 ListView 的通用样式,它的列已经在某些 ListView.View 中定义,我想使用 ItemsControl 作为 headers 部分将使您的风格非常灵活。我相应地修改了我的代码示例,其中 headers 部分是水平方向的 ItemsControl,它的 ItemsSource 绑定到 ListView.View.Columns。这样,样式中的列数将取决于 ListView 中的列数。此外,ItemsControl.ItemTemplate 是一个 TextBlock,它的文本绑定到相应列的 header,因此样式中的列 header 将与列 [=36] 相同=]s 在你的 ListView.