We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to highlight the tapped view in ItemTemplate in Xamarin.Forms ListView (SfListView)

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: March 18, 2020 |
Last Revised Date: April 19, 2020

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

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

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile