gpt4 book ai didi

c# - ListView 的备用行颜色 - uwp

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:07 24 4
gpt4 key购买 nike

如何为 ListView 设置交替行颜色?我将每个项目设置为数据模板。将值绑定(bind)到文本 block 。所以不能遍历 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);
}
}

关于c# - ListView 的备用行颜色 - uwp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47808579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com