Xamarin表格中Gridview行的替代颜色

时间:2016-07-22 20:16:10

标签: styles xamarin.forms

我正在尝试在Xamarin Gridview中制作替代行颜色,不幸的是它不起作用,你能不能看到我在这里犯了什么错误。

我浏览了一些文章,他们建议使用转换器来制作替代的行颜色。

namespace CommonUI.Utilities
{
    public class AlternatingHighlightColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Color rowcolor = Color.Transparent;
            if (value == null || parameter == null) return Color.White;
            var index = ((ListView)parameter).ItemsSource.Cast<object>().ToList().IndexOf(value);
            if (index % 2 == 0)
            {
               rowcolor = Color.Pink;
            }
            return rowcolor;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:U="clr-namespace:CommonUI.Utilities"
         x:Class="CommonUI.Views.MyProfile"
         Title="My Profile" >

  <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" 
           VerticalOptions="FillAndExpand" Padding="2" Spacing="10" BackgroundColor="#DCE4EA">

    <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Spacing="1">
      <Grid BackgroundColor="#CFD9E1" Padding="5">
        <Grid.RowDefinitions>
          <RowDefinition Height="28"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="85"/>
          <ColumnDefinition Width="45"/>
        </Grid.ColumnDefinitions>
        <Label Text="Aircraft" FontSize="13" TextColor="Black" Grid.Row="0" Grid.Column="0" XAlign="Start" YAlign="Center"/>
        <Label Text="Date Created" FontSize="13" TextColor="Black" Grid.Row="0" Grid.Column="1" XAlign="Start" YAlign="Center"/>
        <Label Text="Actions" FontSize="13" TextColor="Black" Grid.Row="0" Grid.Column="2" XAlign="Center" YAlign="Center"/>
      </Grid>
      <ListView x:Name="listView" ItemsSource="{Binding ProfileList}" SeparatorVisibility="Default" SeparatorColor="#DCE4EA">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <Grid BackgroundColor="{Binding .,Converter={StaticResource U:AlternatingHighlightColorConverter}, ConverterParameter={x:Reference listView}}" Padding="5">
                <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>

0 个答案:

没有答案