Articles in this section
Category / Section

How to highlight tapped view in ItemTemplate in Xamarin.Forms ListView?

2 mins read

You can set the background color of each view loaded inside the ItemTemplate when tapped the view in Xamarin Forms ListView.

XAML

<syncfusion:SfListView x:Name="listView"
    ItemSpacing="1" ItemSize="120"
    ItemsSource="{Binding contactsinfo}">
    <syncfusion:SfListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.Behaviors>
                    <local:Behavior/>
                </Grid.Behaviors>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="2"/>
                </Grid.RowDefinitions>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding ContactImage}"/>
                    <StackLayout Grid.Column="1" Padding="0, 10, 0, 0">
                        <Label Text="{Binding ContactName}" VerticalOptions="Center"/>
                        <Label Text="{Binding ContactNumber}" VerticalOptions="Center"/>
                    </StackLayout>
                </Grid>
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Label Text="Availability" Margin="5,0,0,0" FontSize="15" HorizontalOptions="Start" VerticalOptions="Center"/>
                    <Button x:Name="Button1" CornerRadius="10" Text="Busy" FontSize="10" BackgroundColor="{Binding Availability, Converter={StaticResource backgroundColorConverter}, ConverterParameter={x:Reference Button1}}" Grid.Column="1"/>
                    <Button x:Name="Button2" CornerRadius="10" Text="Available" FontSize="10" BackgroundColor="{Binding Availability, Converter={StaticResource backgroundColorConverter}, ConverterParameter={x:Reference Button2}}" Grid.Column="2"/>
                    <Button x:Name="Button3" CornerRadius="10" Text="Away" FontSize="10" BackgroundColor="{Binding Availability, Converter={StaticResource backgroundColorConverter}, ConverterParameter={x:Reference Button3}}" Grid.Column="3"/>
                </Grid>
                <StackLayout Grid.Row="2" BackgroundColor="DarkGray"/>
            </Grid>
        </DataTemplate>
    </syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>

C#

Property updated based on the view content when you tap the view.

public class Behavior : Behavior<Grid>
{
    Button Button1;
    Button Button2;
    Button Button3;
 
    protected override void OnAttachedTo(Grid bindable)
    {
        Button1 = bindable.FindByName<Button>("Button1");
        Button2 = bindable.FindByName<Button>("Button2");
        Button3 = bindable.FindByName<Button>("Button3");
 
        Button1.Clicked += OnClicked;
        Button2.Clicked += OnClicked;
        Button3.Clicked += OnClicked;
 
        base.OnAttachedTo(bindable);
    }
 
    private void OnClicked(object sender, EventArgs e)
    {
        var button = sender as Button;
        var bc = button.BindingContext as Contacts;
 
        if (button.Text == "Yes")
            bc.Availability = true;
        else if (button.Text == "No")
            bc.Availability = false;
        else
            bc.Availability = null;
    }
}

C#

Handled the color of the view by the value of view content.

public class BackgroundColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var button = parameter as Button;
 
        if ((bool?)value == null && button.Text == "Busy")
            return Color.Red;
        else if ((bool?)value == true && button.Text == "Available")
            return Color.YellowGreen;
        else if ((bool?)value == false && button.Text == "Away")
            return Color.Orange;
        else
            return Color.Transparent;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Output

Image for highlight an item when tapped the Xamarin Forms SfListView.

View sample in GitHub


Conclusion

I hope you enjoyed learning about how to highlight tapped view in ItemTemplate in Xamarin.Forms ListView.

You can refer to our Xamarin.Forms Listview feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied