Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
Xamarin XamDataGrid Row Background Color
posted

Hello,

I have a XamDataGrid in use and would like to have several Background Colors for rows, depending on f.e. a viewModels Backgroud Color Property. Here you can see how i defined it in xaml. Can u please give me some hints or sample code to solve my problem?

 <igGrids:TextColumn PropertyPath="F200" 
                                            TextColor="White" 
                                            Background="Gray"
                                            HeaderText="{Binding DataGridHeader.F200}"
                                            IsHidden="{Binding DataGridHeader.F200, Converter={StaticResource StringToHiddenConverter}}">
                            <igGrids:TextColumn.Header>
                                <igGrids:TextHeader FontAttributes="Bold" />
                            </igGrids:TextColumn.Header>
                        </igGrids:TextColumn>

Parents
  • 34430
    Offline posted

    Hello Martin,

    I am under the impression that you are looking to set multiple background colors for different cells in the same column in this case. If this is incorrect, please let me know as the following is based upon this impression.

    Currently, setting the Background color on your TextColumn will blanket each of the cells in that column in the same color, and so binding that property will not help you here. Instead, I would recommend usage of the DataBound event which exists on each column in the Infragistics for Xamarin XamDataGrid.

    In the DataBound event, you can check the event arguments to get the CellInfo for each cell as its underlying data is data bound. This CellInfo element has a RowItem property that you can use to get the underlying data item for your data grid's row. Checking it, you can assign a new Background to your CellInfo object based on your underlying data item or ViewModel. The following example code may help you to achieve this:

            SolidColorBrush yellow = new SolidColorBrush(Color.Yellow);
            SolidColorBrush liteBlue = new SolidColorBrush(Color.LightBlue);
            private void TextColumn_DataBound(object sender, Infragistics.XamarinForms.Controls.Grids.DataBindingEventArgs args)
            {
                if(args.CellInfo != null)
                {
                    var x = args.CellInfo.RowItem;
    
                    if (x != null)
                    {
                        SampleGridData data = x as SampleGridData;
    
                        if (data.ID % 2 == 0)
                        {
                            args.CellInfo.Background = yellow;
                        }
                        else
                        {
                            args.CellInfo.Background = liteBlue;
                        }
                    }
                }
            }

    It is worth noting that the Infragistics for Xamarin XamDataGrid currently styles its rows on a cellular basis, and so if you wish to do this for your entire row, you will need to handle this event for each column in your grid.

    I hope this helps. Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer

Reply Children