|
1 | | -# hover-effect-listview-xamarin-uwp |
2 | | -How to apply hover effect for ListView Item in Xamairn.Forms UWP (SfListView) |
| 1 | +# How to apply hover effect for ListView Item in Xamairn.Forms UWP (SfListView) |
| 2 | + |
| 3 | +You can apply mouse hover effect for [ListViewItem](https://help.syncfusion.com/cr/xamarin/Syncfusion.ListView.XForms.ListViewItem.html) by loading custom control in Xamarin.Forms. To enable the hovering effect, implement a [custom renderer](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/) for the UWP platform. |
| 4 | + |
| 5 | +You can also refer the following article. |
| 6 | + |
| 7 | +https://www.syncfusion.com/kb/11936/how-to-apply-hover-effect-for-listview-item-in-xamairn-forms-uwp-sflistview |
| 8 | + |
| 9 | +**C#** |
| 10 | + |
| 11 | +Define the custom control derived from the [Grid](https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.grid?view=xamarin-forms) in the Xamarin.Forms PCL project. |
| 12 | + |
| 13 | +``` c# |
| 14 | +namespace ListViewXamarin |
| 15 | +{ |
| 16 | + public class CustomGrid : Grid |
| 17 | + { |
| 18 | + |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | +**XAML** |
| 23 | + |
| 24 | +Load the **CustomGrid** in the [SfListView.ItemTemplate](https://help.syncfusion.com/cr/xamarin/Syncfusion.ListView.XForms.SfListView.html#Syncfusion_ListView_XForms_SfListView_ItemTemplate). |
| 25 | + |
| 26 | +``` xml |
| 27 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 28 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 29 | + xmlns:local="clr-namespace:ListViewXamarin" |
| 30 | + xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" |
| 31 | + x:Class="ListViewXamarin.MainPage"> |
| 32 | + <ContentPage.BindingContext> |
| 33 | + <local:ContactsViewModel/> |
| 34 | + </ContentPage.BindingContext> |
| 35 | + <ContentPage.Content> |
| 36 | + <StackLayout> |
| 37 | + <syncfusion:SfListView x:Name="listView" ItemSize="70" SelectionBackgroundColor="#a6dcef" ItemsSource="{Binding ContactsInfo}" SelectionChangedCommand="{Binding ListViewSelection}"> |
| 38 | + <syncfusion:SfListView.ItemTemplate > |
| 39 | + <DataTemplate> |
| 40 | + <local:CustomGrid x:Name="grid"> |
| 41 | + <local:CustomGrid.ColumnDefinitions> |
| 42 | + <ColumnDefinition Width="70" /> |
| 43 | + <ColumnDefinition Width="*" /> |
| 44 | + </local:CustomGrid.ColumnDefinitions> |
| 45 | + <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/> |
| 46 | + <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center"> |
| 47 | + <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/> |
| 48 | + <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/> |
| 49 | + </Grid> |
| 50 | + </local:CustomGrid> |
| 51 | + </DataTemplate> |
| 52 | + </syncfusion:SfListView.ItemTemplate> |
| 53 | + </syncfusion:SfListView> |
| 54 | + </StackLayout> |
| 55 | + </ContentPage.Content> |
| 56 | +</ContentPage> |
| 57 | +``` |
| 58 | +**C#** |
| 59 | + |
| 60 | +Create custom platform renderer for UWP and hook [PointerEntered](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.pointerentered?view=winrt-19041) and [PointerExited](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.pointerexited?view=winrt-19041) events. In the events, you can get the **CustomGrid** from **Element** property and change the [BackgroundColor](https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.visualelement.backgroundcolor?view=xamarin-forms) of the item. Also, you can change the selected item color in the [PointerPressed](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.pointerpressed?view=winrt-19041) event and skip the hovering effect for the selected item based on model class property. |
| 61 | + |
| 62 | +``` c# |
| 63 | +[assembly: ExportRenderer(typeof(CustomGrid), typeof(CustomGridRenderer))] |
| 64 | +namespace ListViewXamarin.UWP |
| 65 | +{ |
| 66 | + public class CustomGridRenderer : VisualElementRenderer<CustomGrid, FrameworkElement> |
| 67 | + { |
| 68 | + public CustomGridRenderer() |
| 69 | + { |
| 70 | + this.PointerEntered += CustomGridRenderer_PointerEntered; |
| 71 | + this.PointerExited += CustomGridRenderer_PointerExited; |
| 72 | + this.PointerPressed += CustomGridRenderer_PointerPressed; |
| 73 | + } |
| 74 | + |
| 75 | + private void CustomGridRenderer_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) |
| 76 | + { |
| 77 | + var item = this.Element; |
| 78 | + item.BackgroundColor = Xamarin.Forms.Color.FromHex("#a6dcef"); |
| 79 | + } |
| 80 | + |
| 81 | + private void CustomGridRenderer_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) |
| 82 | + { |
| 83 | + var item = this.Element; |
| 84 | + item.BackgroundColor = Xamarin.Forms.Color.Transparent; |
| 85 | + } |
| 86 | + |
| 87 | + private void CustomGridRenderer_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) |
| 88 | + { |
| 89 | + var item = this.Element; |
| 90 | + var itemData = item.BindingContext as Contacts; |
| 91 | + if (itemData.IsSelected) |
| 92 | + item.BackgroundColor = Xamarin.Forms.Color.Transparent; |
| 93 | + else |
| 94 | + item.BackgroundColor = Xamarin.Forms.Color.WhiteSmoke; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | +**C#** |
| 100 | + |
| 101 | +In the [SelectionChangedCommand](https://help.syncfusion.com/cr/xamarin/Syncfusion.ListView.XForms.SfListView.html#Syncfusion_ListView_XForms_SfListView_SelectionChangedCommand), update the **IsSelected** model property to update the selection color in platform renderer. |
| 102 | + |
| 103 | +``` c# |
| 104 | +namespace ListViewXamarin |
| 105 | +{ |
| 106 | + public class ContactsViewModel : INotifyPropertyChanged |
| 107 | + { |
| 108 | + public Command<object> ListViewSelection { get; set; } |
| 109 | + |
| 110 | + public ContactsViewModel() |
| 111 | + { |
| 112 | + ListViewSelection = new Command<object>(OnItemSelected); |
| 113 | + } |
| 114 | + |
| 115 | + private void OnItemSelected(object obj) |
| 116 | + { |
| 117 | + var args = obj as Syncfusion.ListView.XForms.ItemSelectionChangedEventArgs; |
| 118 | + if (args.AddedItems.Count > 0) |
| 119 | + { |
| 120 | + var item = args.AddedItems[0] as Contacts; |
| 121 | + item.IsSelected = true; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + var item = args.RemovedItems[0] as Contacts; |
| 126 | + item.IsSelected = false; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | +**Output** |
| 133 | + |
| 134 | + |
0 commit comments