28

我正在使用基于 itemTemplate 的 listView。所以我需要在我的模板中交替背景颜色:
- 第一行:白色
- 第二行:灰色
- 第三行:白色
- 第四行:灰色

这是我的模板:

     <DataTemplate x:Key="ItemFlight" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Border Background="#28AADB" Margin="2">
                <Image Source="{Binding Path=IsArrival, Converter={StaticResource BooleanToImageDisplayConverter}}" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
            </Border>
            <Grid Grid.Column="1" VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="3*"/>
                    <ColumnDefinition Width="3*"/>
                    <ColumnDefinition Width="6*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding FlightName}" FontWeight="Bold" Grid.Column="0" Grid.Row="0"  Margin="10" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding ArrivalOrDepartDateTime, Converter={StaticResource DateTimeConverter}}"  FontWeight="Bold" Grid.Column="0" Grid.Row="1" Margin="10" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding Terminal, Converter={StaticResource StringUpperConverter}}" Grid.Column="1" Grid.Row="0" Margin="10"  Visibility="{Binding Path=IsArrival,Converter={StaticResource BooleanToVisibilityReverseConverter}}" Style="{StaticResource MyTextBlockStyle}"/>
                <TextBlock Text="{Binding CityInfo.Name}" Grid.Column="1" Grid.Row="0" Margin="10" Visibility="{Binding Path=IsArrival,Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding DepartureTime}"  Grid.Column="1" Grid.Row="1" Margin="10" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding CityInfo.Name}" Grid.Column="2" Grid.Row="0" Margin="10" Style="{StaticResource MyTextBlockStyle}"  Visibility="{Binding Path=IsArrival,Converter={StaticResource BooleanToVisibilityReverseConverter}}"/>
                <TextBlock Text="{Binding Terminal, Converter={StaticResource StringUpperConverter}}"  Visibility="{Binding Path=IsArrival,Converter={StaticResource BooleanToVisibilityConverter}}" Grid.Column="2" Grid.Row="0" Margin="10" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding ArrivalTime}" Grid.Column="2" Grid.Row="1" Margin="10" Style="{StaticResource MyTextBlockStyle}"/>

                <TextBlock Text="{Binding Status}" Grid.Column="3" Grid.Row="0" Grid.RowSpan="2" Margin="15" Style="{StaticResource MyTextBlockStyle}"  Foreground="#EA6A1E" FontSize="20" TextWrapping="Wrap" />

            </Grid>
        </Grid>
    </DataTemplate>

请问我该怎么做??

4

3 回答 3

55

我试过这个,它对我有用。

<Window x:Class="TryResponses.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:TryResponses"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <vm:MainWindowViewModel x:Key="MainWindowViewModel" />
</Window.Resources>
<Grid Background="LightGray" DataContext="{StaticResource MainWindowViewModel}">
    <Grid.Resources>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                    <Setter Property="Background" Value="LightGray" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <DataTemplate DataType="system:String">
            <!-- put your data template here -->
        </DataTemplate>
    </Grid.Resources>
    <ListView ItemsSource="{Binding Items}" AlternationCount="2" />
</Grid>

我希望这将有所帮助。

问候

克劳德

于 2013-07-04T19:45:39.973 回答
5

您应该使用 AlternationCount 属性,它适用于 ListBox、ListView 或任何其他从 ItemsControl 继承的控件。属性定义和两个示例包含在
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount%28v=vs.110%29.aspx中)

于 2015-06-09T14:20:24.240 回答
0

要更清楚地查看所选线条,您可以尝试以下操作:(不要在意颜色和最终渲染,我没有花必要的时间让它变得性感)

<Grid DataContext="{StaticResource MainWindowViewModel}">
    <Grid.Resources>
        <local:FalseToCollapsedConverter x:Key="FalseToCollapsedConverter" />
    </Grid.Resources>
    <ListView ItemsSource="{Binding Items}" AlternationCount="2" HorizontalContentAlignment="Stretch">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Grid x:Name="line">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <ContentControl Content="{Binding .}" Margin="4" />
                                <TextBlock Grid.Column="1" Text="V" Margin="4" Visibility="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem, Mode=FindAncestor}, Converter={StaticResource FalseToCollapsedConverter}}" />
                                <Border Grid.ColumnSpan="2" Background="#5500FF00" 
                                        BorderBrush="Blue" BorderThickness="2"
                                        Visibility="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem, Mode=FindAncestor}, Converter={StaticResource FalseToCollapsedConverter}}" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontWeight" Value="Bold" />
                                </Trigger>
                                <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                                    <Setter TargetName="line" Property="Background" Value="#CCCCFF" />
                                </Trigger>
                                <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                                    <Setter TargetName="line" Property="Background" Value="#CCFFCC" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>
    </ListView>
</Grid>
于 2021-06-25T23:31:11.033 回答