簡體   English   中英

列表視圖的備用行顏色-UWP

[英]Alternate row color for List View - uwp

如何為列表視圖設置備用行顏色? 我將每個項目設置為datatemplate.Binding值綁定到textblock。 所以不能遍歷listview項。如何實現?

 <ListView x:Name="list1"  Width="300" Height="500" Background="White"  Loaded="list1_Loaded"   Style="{StaticResource FixedHeaderListViewStyle}">
            <ListView.ItemTemplate>
                <DataTemplate >
                    <Grid Tapped="StackPanel_Tapped" Width="300" Height="38">                    
                    <Border  Width="390" Height="38"  HorizontalAlignment="Stretch">
                    <TextBlock  Text="{Binding ItemUserName}" Padding="4" TextWrapping="Wrap"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left" Foreground="White"/>
                    </Border>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

  private void list1_Loaded(object sender, RoutedEventArgs e)
    {  
        ListView listView = sender as ListView;
        ItemCollection ic = listView.Items;
        int counter = 1;
        foreach (ListViewItem item in ic)
        {
            if (counter % 2 == 0)
            {
                item.Background = new SolidColorBrush(Colors.Orange);
                item.Foreground = new SolidColorBrush(Colors.DarkRed);
            }
            else
            {
                item.Background = new SolidColorBrush(Colors.OrangeRed);
                item.Foreground = new SolidColorBrush(Colors.Snow);
            }
            counter++;
        }

    }

得到如下解決方案

void BackgroundAlternatingListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
    if (args.ItemIndex % 2 != 0)
    {
        args.ItemContainer.Background = new SolidColorBrush(_secondColor);
    }
    else
    {
        args.ItemContainer.Background = new SolidColorBrush(_startColor);
    }
}

只需使用<AlternatingItemTemplate>並為其設置不同的顏色,就不需要控制器上的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM