Giter Site home page Giter Site logo

syncfusionexamples / hover-effect-listview-xamarin-uwp Goto Github PK

View Code? Open in Web Editor NEW
0.0 4.0 0.0 2.18 MB

This repository contains sample about how to apply hover effect for ListView Item in Xamairn.Forms UWP (SfListView)

C# 100.00%
listview xamarin xamarin-forms sflistview mouse-hover

hover-effect-listview-xamarin-uwp's Introduction

How to apply hover effect for ListView Item in Xamairn.Forms UWP (SfListView)

You can apply mouse hover effect for ListViewItem by loading custom control in Xamarin.Forms. To enable the hovering effect, implement a custom renderer for the UWP platform.

You can also refer the following article.

https://www.syncfusion.com/kb/11936/how-to-apply-hover-effect-for-listview-item-in-xamairn-forms-uwp-sflistview

C#

Define the custom control derived from the Grid in the Xamarin.Forms PCL project.

namespace ListViewXamarin
{
    public class CustomGrid : Grid
    {
        
    }
}

XAML

Load the CustomGrid in the SfListView.ItemTemplate.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewXamarin"
             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             x:Class="ListViewXamarin.MainPage">
    <ContentPage.BindingContext>
        <local:ContactsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <syncfusion:SfListView x:Name="listView" ItemSize="70" SelectionBackgroundColor="#a6dcef" ItemsSource="{Binding ContactsInfo}" SelectionChangedCommand="{Binding ListViewSelection}">
                <syncfusion:SfListView.ItemTemplate >
                    <DataTemplate>
                        <local:CustomGrid x:Name="grid">
                            <local:CustomGrid.ColumnDefinitions>
                                <ColumnDefinition Width="70" />
                                <ColumnDefinition Width="*" />
                            </local:CustomGrid.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">
                                <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/>
                                <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                            </Grid>
                        </local:CustomGrid>
                    </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>
            </syncfusion:SfListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

C#

Create custom platform renderer for UWP and hook PointerEntered and PointerExited events. In the events, you can get the CustomGrid from Element property and change the BackgroundColor of the item. Also, you can change the selected item color in the PointerPressed event and skip the hovering effect for the selected item based on model class property.

[assembly: ExportRenderer(typeof(CustomGrid), typeof(CustomGridRenderer))]
namespace ListViewXamarin.UWP
{
    public class CustomGridRenderer : VisualElementRenderer<CustomGrid, FrameworkElement>
    {
        public CustomGridRenderer()
        {
            this.PointerEntered += CustomGridRenderer_PointerEntered;
            this.PointerExited += CustomGridRenderer_PointerExited;
            this.PointerPressed += CustomGridRenderer_PointerPressed;
        }

        private void CustomGridRenderer_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var item = this.Element;
            item.BackgroundColor = Xamarin.Forms.Color.FromHex("#a6dcef");
        }

        private void CustomGridRenderer_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var item = this.Element;
            item.BackgroundColor = Xamarin.Forms.Color.Transparent;
        }

        private void CustomGridRenderer_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var item = this.Element;
            var itemData = item.BindingContext as Contacts;
            if (itemData.IsSelected)
                item.BackgroundColor = Xamarin.Forms.Color.Transparent;
            else
                item.BackgroundColor = Xamarin.Forms.Color.WhiteSmoke;
        }
    }
}

C#

In the SelectionChangedCommand, update the IsSelected model property to update the selection color in platform renderer.

namespace ListViewXamarin
{
    public class ContactsViewModel : INotifyPropertyChanged
    {
        public Command<object> ListViewSelection { get; set; }

        public ContactsViewModel()
        {
            ListViewSelection = new Command<object>(OnItemSelected);
        }

        private void OnItemSelected(object obj)
        {
            var args = obj as Syncfusion.ListView.XForms.ItemSelectionChangedEventArgs;
            if (args.AddedItems.Count > 0)
            {
                var item = args.AddedItems[0] as Contacts;
                item.IsSelected = true;
            }
            else
            {
                var item = args.RemovedItems[0] as Contacts;
                item.IsSelected = false;
            }
        }
    }
}

Output

HoverEffectListView

hover-effect-listview-xamarin-uwp's People

Contributors

jayaleshwari avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 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.