'Changing BackgroundColor of Button within Xamarin CollectionView changes the BackgroundColor of every 8th button down. why?

I have a Xamarin CollectionView That uses a Button (Button x:Name="PNameButton") to Change the Background color of the button.
When the Button is clicked the background color changes... However - So does every 8th Button in the CollectionView. Its as if the CollectionView renders new data every 8 or so items, and that new first button gets the changed color property. How do I fix this? Code Follows:

            <CollectionView x:Name="PairingsCollectionView" SelectionMode="Multiple" Margin="20,5,20,5"  >
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        

                        <Grid Margin="0" Padding="0,0,0,0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="25" />
                                <RowDefinition Height="35" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                
                                
                            </Grid.ColumnDefinitions>
                            <BoxView Grid.Column="0" Grid.Row="0" Margin="0" Color="#333130"/>
                            <Button x:Name="PNameButton" Margin="0" Padding="0,0,0,0" Grid.Column="0" BackgroundColor="{Binding UseColor}" Text="{Binding PDisplayName}" FontSize="Small" TextColor="Black" 
                                   HorizontalOptions="Start" VerticalOptions="Center" Clicked="Button_Clicked" />
                            <BoxView Grid.Column="1" Grid.Row="0" Color="#333130" Margin="0"></BoxView>
                            <Label Margin="0" Padding="0,0,0,0" Grid.Column="1" TextColor="White" FontSize="Small" 
                                HorizontalOptions="Center" VerticalOptions="Center" >
                                <Label.FormattedText>
                                    <FormattedString>
                                        <Span Text="{Binding PDays, StringFormat='{0}Dy'}"/>
                                        <Span Text=" "/>
                                        <Span Text="{Binding PDay}"/>
                                    </FormattedString>
                                </Label.FormattedText>
                            </Label>
                            <BoxView Grid.Row="0" Grid.Column="2" Margin="0" Color="#333130"></BoxView>
                            <Label  Margin="0" Padding="0,0,0,0" Grid.Column="2" Text="{Binding PCredit, StringFormat='{0}'}" TextColor="#F57BFA" FontSize="Small" 
                                    HorizontalOptions="Center" VerticalOptions="Center"/>
                            <BoxView Grid.Row="0" Grid.Column="3" Color="#333130" Margin="0"></BoxView>
                            <Label Margin="0" Padding="0,0,0,0"  Grid.Column="3" Text="{Binding PFlightTime, StringFormat='{0}'}" TextColor="White" FontSize="Small" 
                                   HorizontalOptions="Center"  VerticalOptions="Center"/>
                            <BoxView Grid.Row="0" Grid.Column="4" Color="#333130" Margin="0"/>
                            <Label Margin="0" Padding="0,0,0,0"  Grid.Column="4" Text="{Binding PRegionType, StringFormat='{0}'}" TextColor="White" FontSize="Small" HorizontalOptions="Center" VerticalOptions="Center"/>
                            <BoxView Grid.Row="0" Grid.Column="5" Color="#333130"  Margin="0"/>
                            <CheckBox x:Name="IsSelectedCheckbox" Grid.Row="0" Grid.Column="5" IsChecked="{Binding PIsSelected}" VerticalOptions="Center"/>
                            

                            <BoxView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Color="LightGray"></BoxView>
                            <Label Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Text="{Binding PDateModified}" TextColor="Black" FontSize="Small" 
                                   HorizontalOptions="Center" VerticalOptions="Center"/>
                            <BoxView Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" Color="LightGray" ></BoxView>
                            
                            <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" Text="{Binding PLayovers}"
                            TextColor="Coral" FontAttributes="Bold"  FontSize="17" HorizontalOptions="Start" VerticalOptions="Center" Margin="5,0,0,0" />


                            <WebView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="6" BackgroundColor="#333130" 
                                     MinimumHeightRequest="150" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" 
                                     HeightRequest="{Binding PWebViewHeight}" >
                                <WebView.Source>
                                    <HtmlWebViewSource Html="{Binding PAllText}"/>
                                </WebView.Source>
                                
                            </WebView>
                                                 
                        </Grid>

                        
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

And in the Code Behind:

private void Button_Clicked(object sender, EventArgs e)
{
    try
    {
        string ChkVal = "";
        bool compareBool = false;
        bool currentState = false;
        Color buttonColer;
        string ColorHexVal;
        int i = 0;



        var thisButton = sender as Button;
        

        buttonColer = thisButton.BackgroundColor;
        ColorHexVal = buttonColer.ToHex();
        if (buttonColer != Color.FromHex("#F7F75A"))  
        {   
            currentState = true;
        }
        else
        {
            currentState = false;
        }
        
        var PairingItem = ((Button)sender).Parent.BindingContext as Pairings;
        compareBool = App.DatabaseNA.GetIsSelected(PairingItem.PID);


        if (!compareBool == true)
            {

            thisButton.BackgroundColor = Color.FromHex("#46AE3C"); 
        }
            else
            {

            thisButton.BackgroundColor = Color.FromHex("#F7F75A");

        }

    }
    catch (Exception ex)
    {
        DisplayAlert("Alert", ex.InnerException.ToString(), "OK"); //await 

    }

}

Again - Thank You!!! The code works great and sets the Background color of the button... The problem is - It sets the Background Color of every Button about 8 items down.

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source