C# 列表视图的替代行颜色-uwp
如何为listview设置备用行颜色?我正在将每个项目设置为datatemplate。将值绑定到textblock。所以不能遍历listview项。我如何实现它C# 列表视图的替代行颜色-uwp,c#,xaml,uwp,C#,Xaml,Uwp,如何为listview设置备用行颜色?我正在将每个项目设置为datatemplate。将值绑定到textblock。所以不能遍历listview项。我如何实现它 <ListView x:Name="list1" Width="300" Height="500" Background="White" Loaded="list1_Loaded" Style="{StaticResource FixedHeaderListViewStyle}"> <
<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++;
}
}
已加载私有无效列表1_(对象发送方,路由目标)
{
ListView ListView=发件人作为ListView;
ItemCollection ic=listView.Items;
int计数器=1;
foreach(ic中的ListViewItem)
{
如果(计数器%2==0)
{
item.Background=新的SolidColorBrush(颜色为橙色);
item.前台=新的SolidColorBrush(Colors.DarkRed);
}
其他的
{
item.Background=新的SolidColorBrush(颜色为橙色);
item.前台=新的SolidColorBrush(Colors.Snow);
}
计数器++;
}
}
只需使用
并为其设置不同的颜色,控制器上不需要代码。获得如下解决方案
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);
}
}
您的列表是否有
橙色
和雪色
的颜色?或者根本不包含任何颜色?尝试并调试计数器,以查看其是否正常工作。控件将不进入foreach(ic中的ListViewItem)循环井将问题更改为foreach不工作,然后。。你确定ic有任何数据吗?您是遇到异常还是它没有进入循环?AlternatingItemTemplate
在UWP中不受支持