0

Where do we set a default color #c4bfc7 for row records displaying in the page, below is the xaml. Now while tapping, it is showing some color, which is fine. But I want to display some color for rows by default

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="2*"/>
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding image}" WidthRequest="50" HeightRequest="50" Grid.Column="0" VerticalOptions="Center"/>
                        <StackLayout Grid.Column="1">
                            <Label Text="{Binding FullName}" TextColor="White" HorizontalTextAlignment="Start"/>
                        </StackLayout>
                        <StackLayout Grid.Column="2">
                            <Label Text="{Binding SoccerStatus}" HorizontalTextAlignment="Center" TextColor="White"/>
                        </StackLayout>
                        <StackLayout Grid.Column="3">
                            <Label Text="{Binding CurrentDate}" HorizontalTextAlignment="Center" TextColor="White"/>
                        </StackLayout>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
soccerway
  • 4,936
  • 7
  • 32
  • 62

2 Answers2

3

UPDATE

Your grid inside the ViewCell would look something like below:

<Grid BackgroundColor="#c4bfc7">

First of all, you might wanna add a property for hex code in your model that is bound to your ListView

public class ListModel
{
   public string HexCode{get; set;}
}

Now when you are filling this model based on your conditions push in the correct hex code that you want as that items background colour. Once you are done with that your listview would look something like below:

<ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid BackgroundColor={Binding HexCode}>

Then add a convertor that converts hex to colour:

public class HexToColorConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string colorHex = (string)value;

        if (string.IsNullOrEmpty(colorHex) && string.IsNullOrWhiteSpace(colorHex))
            return Color.White;

        return Color.FromHex(colorHex); //Note that if the hex code is not valid this might crash

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Not implemented as we do not convert back
        throw new NotSupportedException();
    }
}

Now add this convertor to your resources like below:

<common:HexToColorConvertor  x:Key="HexToColor" />

Where common is the namespace of that convertor and then use it like below:

<ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid BackgroundColor={Binding HexCode, Converter={StaticResource HexToColor}}>
FreakyAli
  • 9,662
  • 3
  • 16
  • 45
0

You should set #c4bfc7 background colour to outmost view which is is grid inside view cell here.

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid BackgroundColor="#c4bfc7">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="2*"/>
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding image}" WidthRequest="50" HeightRequest="50" Grid.Column="0" VerticalOptions="Center"/>
                        <StackLayout Grid.Column="1">
                            <Label Text="{Binding FullName}" TextColor="White" HorizontalTextAlignment="Start"/>
                        </StackLayout>
                        <StackLayout Grid.Column="2">
                            <Label Text="{Binding SoccerStatus}" HorizontalTextAlignment="Center" TextColor="White"/>
                        </StackLayout>
                        <StackLayout Grid.Column="3">
                            <Label Text="{Binding CurrentDate}" HorizontalTextAlignment="Center" TextColor="White"/>
                        </StackLayout>
                    </Grid>
                </ViewCell>
            </DataTemplate>
</ListView.ItemTemplate>

Furthermore If you want to change the list view selected item color. Here is an answer which worked for me. It includes adding few keys to android's style.xml https://stackoverflow.com/a/38457882/4707196

prasadsunny1
  • 557
  • 3
  • 10