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
15
XamDataGrid Row Background Color Doesn't Refresh on Sort
posted

Hello,

I have a XamDataGrid that sets the background color depending on a status property within a model. The background is set correctly on start up but when I sort the columns using the built in sorter, the background color does not update. This causes some rows to have the wrong color and it does not correct itself until I scroll them out of view.

Here is my code below:

XAML View

<grids:XamDataGrid x:Name="dataGrid" AutoGenerateColumns="False" Grid.Row="1" Grid.RowSpan="11" RowHeight="40" SelectionMode="SingleRow" SelectedItemsChanged="dataGrid_SelectedItemsChanged" SortDescriptionsChanged="ApplySort">
    <grids:XamDataGrid.Columns>
        <grids:TextColumn PropertyPath="Col1" Width="*" DataBound="StylizeRowColor" HeaderText="Col 1" />
        <grids:TextColumn PropertyPath="Col2" Width="3*" DataBound="StylizeRowColor" LineBreakMode="WordWrap" IsFilteringEnabled="True"/>
        <grids:TextColumn PropertyPath="Col3" Width="*" DataBound="StylizeRowColor" IsFilteringEnabled="True"/>
        <grids:NumericColumn PropertyPath="Col4" Width="*" HorizontalAlignment="Center" DataBound="StylizeRowColor" IsFilteringEnabled="True"/>
    </grids:XamDataGrid.Columns>
</grids:XamDataGrid>




C# Code Behind
private List<DataModel> DataList;
private ColumnSortDescription DataSorter;
public SpreadsheetView()
{
    InitializeComponent();
    DataSorter = new ColumnSortDescription("Col1", Infragistics.Core.Controls.DataSource.ListSortDirection.Ascending);
    DataList = GetDataList();
    dataGrid.ItemsSource = DataList;
    dataGrid.SortDescriptions.Add(DataSorter);
    dataGrid.SortDescriptions.PropertyChanged += ApplySorting;
    dataGrid.ReactsToSortChanges = true;
}

public List<SlotMachine> GetDataList()
{
    return DataService.GetData();
}

private void StylizeRowColor(object sender, DataBindingEventArgs args)
{
    if (args.CellInfo != null)
    {
        var rowItem = args.CellInfo.RowItem;
        if (rowItem != null)
        {
            DataModel item = rowItem as DataModel;

            switch (item.Status)
            {
                case "InService":
                    break;
                case "OutOfService":
                    args.CellInfo.Background = new Infragistics.XamarinForms.SolidColorBrush(Color.Red);
                    break;
                default:
                    break;
            }
        }
    }
}

private void ApplySorting(object sender, PropertyChangedEventArgs e)
{
    if (dataGrid.ActualDataSource != null)
    {
        dataGrid.ActualDataSource.QueueAutoRefresh();
    }
}

  • 33240
    Offline posted

    Hello Rafael,

    I have been investigating into the behavior you are seeing, and it sounds like there are some cells that are likely recycling themselves potentially into the same cell, and so when you sort, they will retain the same Background.

    In order to prevent this, I would recommend that you set the args.CellInfo.Background property in your default and “InService” cases in your StylizeRowColor event handler. You can set the args.CellInfo.Background back to a default background color to ensure that you prevent this behavior from happening.

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