Articles in this section
Category / Section

How to apply the background color when mouse hover on SfDataGrid in Xamarin.Forms ?

3 mins read

By default, the SfDataGrid does not have a support to apply the row background color when hover on the row in UWP platform. But we can achieve this by hosting the SfDataGrid into the custom StackLayout and customize the StackLayout renderer.

 

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:DataGridDemo"
             xmlns:data="clr-namespace:Syncfusion.Data;assembly=Syncfusion.Data.Portable"
             xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
             xmlns:sfPager="clr-namespace:Syncfusion.SfDataGrid.XForms.DataPager;assembly=Syncfusion.SfDataGrid.XForms"
             x:Class="DataGridDemo.MainPage">
    <ContentPage.Content>
        <local:CustomStackLayout Orientation="Vertical">
            <sfgrid:SfDataGrid x:Name="sfgrid" AllowResizingColumn="True" AllowSwiping="True">
 
            </sfgrid:SfDataGrid>
        </local:CustomStackLayout>
 
    </ContentPage.Content>
</ContentPage>


public class CustomStackLayout : StackLayout
    {
        public CustomStackLayout()
        {
 
        }
 
        protected override void LayoutChildren(double x, double y, double width, double height)
        {
            base.LayoutChildren(x, y, width, height);
        }
    }


//UWP Renderer
public class CustomStackLayoutRenderer : VisualElementRenderer<CustomStackLayout, Control>
    {
 
        int previousIndex;
 
        DataRowBase rowdata;
 
        VirtualizingCellsControl wholeRowElement;
 
        SfDataGrid grid;
 
        Xamarin.Forms.Point point;
        public CustomStackLayoutRenderer()
        {
            this.PointerMoved += CustomStackLayoutRenderer_PointerMoved;
            this.PointerReleased += CustomStackLayoutRenderer_PointerReleased;
        }
 
        private void CustomStackLayoutRenderer_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var pointerPoint = e.GetCurrentPoint(this);
            point.Y = pointerPoint.Position.Y;
            point.X = pointerPoint.Position.X;
            var rowColumnIndex = SfDataGridHelpers.PointToCellRowColumnIndex(grid, point);
            var model = (GridModel)grid.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("GridModel")).GetValue(grid);
            rowdata = SfDataGridHelpers.GetRowGenerator(grid).Items.FirstOrDefault(x => x.RowIndex == rowColumnIndex.RowIndex);
            if (rowdata != null)
            {
                wholeRowElement = (VirtualizingCellsControl)rowdata.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement")).GetValue(rowdata);
                wholeRowElement.BackgroundColor = Xamarin.Forms.Color.White;
            }
        }
 
        private void CustomStackLayoutRenderer_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var pointerPoint = e.GetCurrentPoint(this);
            point.Y = pointerPoint.Position.Y;
            point.X = pointerPoint.Position.X;
            var rowColumnIndex = SfDataGridHelpers.PointToCellRowColumnIndex(grid, point);
            var model = (GridModel)grid.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("GridModel")).GetValue(grid);
            if (previousIndex == rowColumnIndex.RowIndex)
            {
 
                rowdata = SfDataGridHelpers.GetRowGenerator(grid).Items.FirstOrDefault(x => x.RowIndex == rowColumnIndex.RowIndex);
                if (rowdata != null)
                {
                    wholeRowElement = (VirtualizingCellsControl)rowdata.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement")).GetValue(rowdata);
                    wholeRowElement.BackgroundColor = Xamarin.Forms.Color.Red;
                }
            }
            else
            {
                rowdata = SfDataGridHelpers.GetRowGenerator(grid).Items.FirstOrDefault(x => x.RowIndex == previousIndex);
                if (rowdata != null)
                {
                    wholeRowElement = (VirtualizingCellsControl)rowdata.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement")).GetValue(rowdata);
                    wholeRowElement.BackgroundColor = Xamarin.Forms.Color.White;
                }
            }
            previousIndex = rowColumnIndex.RowIndex;
 
        }
 
        protected override void OnElementChanged(ElementChangedEventArgs<CustomStackLayout> e)
        {
            base.OnElementChanged(e);
            grid = this.Element.Children[0] as SfDataGrid;
            grid.GridTapped += Grid_GridTapped;
            point = new Xamarin.Forms.Point();
 
        }
 
        private void Grid_GridTapped(object sender, GridTappedEventArgs e)
        {
            wholeRowElement = (VirtualizingCellsControl)rowdata.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement")).GetValue(rowdata);
            wholeRowElement.BackgroundColor = Xamarin.Forms.Color.Blue;
        }
    }


SfDataGrid UWP

 

View Sample in GitHub

Conclusion

I hope you enjoyed learning about how to apply the background color when mouse hover on SfDataGrid in Xamarin.Forms.

You can refer to our Xamarin.Forms DataGrid featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

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