用于 ListView 的 DataTemplate 中的行分隔符

JuP*_*JuP 2 xaml windows-runtime winrt-xaml uwp uwp-xaml

我有 ItemsControl,每个项目都有可绑定的 itemsource 和自定义 datetemplate。项目由行分隔。但是最后一项也有分隔符,这就是我的问题,如何不为最后一项渲染行。我找到了这个解决方案,但它在 WPF 中有效:

如何在 ItemsControl 中的项目之间添加分隔符

编辑: 这是我的模板:

<ItemsControl Grid.Row="1"  ItemsSource="{x:Bind ViewModel.AvailableStatuses}" x:Name="Statuses">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" Padding="60,0,60,12"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="Transparent">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <i:Interaction.Behaviors>
                                    <core:EventTriggerBehavior EventName="Tapped">
                                        <core:InvokeCommandAction Command="{Binding ElementName=ContentGrid, Path=DataContext.ChangeStatusCommand}" CommandParameter="{Binding}"/>
                                    </core:EventTriggerBehavior>
                                </i:Interaction.Behaviors>
                                <Rectangle StrokeThickness="0.4" Height="0.4" x:Name="Separator" 
                                   VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Stroke="#D1D3D4" />
                                <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Stretch">
                                    <Image Source="{Binding Converter={StaticResource SelectContactStatusConverter}}" Margin="0,8,12,8"/>
                                    <TextBlock Text="{Binding Converter={StaticResource EnumContactStatusToTextConverter}}" FontSize="20" VerticalAlignment="Center" Foreground="Black"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
Run Code Online (Sandbox Code Playgroud)

Dec*_*oon 5

可能有几种不同的方法可以做到这一点,这是我的看法。

使用您想要的边框画笔和粗细为 ListView 的项目容器设置样式,并订阅 ContainerContentChanging 事件:

<ListView ContainerContentChanging="ListView_ContainerContentChanging">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="0,1,0,0"/>
        </Style>
    </ListView.ItemContainerStyle>

    <x:String>1</x:String>
    <x:String>2</x:String>
    <x:String>3</x:String>
    <x:String>4</x:String>
</ListView>
Run Code Online (Sandbox Code Playgroud)

在你后面的代码中:

<ListView ContainerContentChanging="ListView_ContainerContentChanging">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="0,1,0,0"/>
        </Style>
    </ListView.ItemContainerStyle>

    <x:String>1</x:String>
    <x:String>2</x:String>
    <x:String>3</x:String>
    <x:String>4</x:String>
</ListView>
Run Code Online (Sandbox Code Playgroud)

我决定采用这种方法有几个原因:

  • 将边框样式应用于 ListViewItem 而不是在 ItemTemplate 中是有意义的,因为无论哪个项目模板用于特定项目,它对于列表中的所有项目都是相同的。此外,它将确保该线触及 ListView 的左右边缘。
  • 可以在 XAML 中调整分隔符的视觉外观。如果您想重用代码隐藏,可以将其放入行为(或 ListView 的子类)中。
  • 即使从列表中添加/删除项目,或重新排序项目,它也会保持正确的外观。
  • 它适用于 UI 虚拟化。
  • 它独立于绑定到列表视图的数据。

截屏


编辑

看起来您的意思是 ItemsControl,而不是 ListView。如果是这种情况,那么您必须按照 Depechie 的回答做一些事情,因为 ItemsControl 没有 ContainerContentChanging 事件(子类 ItemsControl 并覆盖 PrepareContainerForItemOverride),但这可能不适用于添加和删除项目的动态列表运行。如果这对您很重要,您必须尝试不同的解决方案。