Giter Site home page Giter Site logo

syncfusionexamples / selection-text-color-listview-xamarin Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 0.0 1.88 MB

This repository contains sample about how to change the ListView selected item text color in Xamarin.Forms (SfListView)

C# 100.00%
listview sflistview selected-item xamarin xamarin-forms selected-item-text-color

selection-text-color-listview-xamarin's Introduction

How to change the ListView selected item text color in Xamarin.Forms (SfListView)

In Xamarin.Forms ListView, you can change the text color of selected item by using SelectionChanging event.

You can also refer the following article.

https://www.syncfusion.com/kb/11363/how-to-change-the-listview-selected-item-text-color-in-xamarin-forms-sflistview

C#

TextColor updated in SelectionChanging event, based on selection added or removed.

public class Behavior : Behavior<SfListView>
{
    SfListView listView;
    protected override void OnAttachedTo(SfListView bindable)
    {
        listView = bindable;
        listView.SelectionChanging += ListView_SelectionChanging;
        base.OnAttachedTo(bindable);
    }
 
    private void ListView_SelectionChanging(object sender, ItemSelectionChangingEventArgs e)
    {
        if (listView.SelectionMode == Syncfusion.ListView.XForms.SelectionMode.Single)
        {
            if (e.AddedItems.Count > 0)
            {
                var item = e.AddedItems[0] as Contacts;
                item.LabelTextColor = Color.Red;
            }
            if (e.RemovedItems.Count > 0)
            {
                var item = e.RemovedItems[0] as Contacts;
                item.LabelTextColor = Color.Black;
            }
        }
    }
 
    protected override void OnDetachingFrom(SfListView bindable)
    {
        listView.SelectionChanging -= ListView_SelectionChanging;
 
        base.OnDetachingFrom(bindable);
    }
}

XAML

LabelTextColor bound to the Label added to SfListView in the ItemTemplate and Behaviour.

<syncfusion:SfListView x:Name="listView" ItemSpacing="1" SelectionMode="Single"
                        SelectionBackgroundColor="LightGray" AutoFitMode="Height" 
                        ItemsSource="{Binding contactsinfo}">
    <syncfusion:SfListView.Behaviors>
        <local:Behavior/>
    </syncfusion:SfListView.Behaviors>
    <syncfusion:SfListView.ItemTemplate >
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <Grid x:Name="grid" RowSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="1" />
                        </Grid.RowDefinitions>
                        <Grid RowSpacing="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="70" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
 
                            <Image Source="{Binding ContactImage}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    HeightRequest="50" WidthRequest="50"/>
 
                            <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
 
                                <Label LineBreakMode="NoWrap"  Text="{Binding ContactName}" 
                                        TextColor="{Binding LabelTextColor}"/>
                                <Label Grid.Row="1" Grid.Column="0"
                                        TextColor="{Binding LabelTextColor}"
                                        LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                            </Grid>
                        </Grid>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>

Output

SelectionTextColor

selection-text-color-listview-xamarin's People

Contributors

jayaleshwari avatar sarubala20 avatar syncfusionbuild avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.