Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Grid ] Index was out of range. Must be non-negative and less than the size of the collection. #4263

Closed
FoggyFinder opened this issue Jul 11, 2020 · 2 comments · Fixed by #4295

Comments

@FoggyFinder
Copy link
Contributor

I saw the exception before in my app but wasn't able to provide MCVE. And now finally I got it when I was working on a sample for DatePicker.

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <DatePicker
            DayVisible="{Binding ElementName=dCh, Path=IsChecked}"
            MonthVisible="{Binding ElementName=mCh, Path=IsChecked}"
            YearVisible="{Binding ElementName=yCh, Path=IsChecked}" />
        <Grid ColumnDefinitions="*,*,*">
            <CheckBox
                Name="dCh"
                Grid.Column="0"
                Content="Day"
                IsChecked="True" />
            <CheckBox
                Name="mCh"
                Grid.Column="1"
                Content="Month"
                IsChecked="True" />
            <CheckBox
                Name="yCh"
                Grid.Column="2"
                Content="Year"
                IsChecked="True" />
        </Grid>
    </StackPanel>

Steps to reproduce:

  1. Uncheck last CheckBox (year)

StackTrace

   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Avalonia.Collections.AvaloniaList`1.get_Item(Int32 index)
   at Avalonia.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
   at Avalonia.Controls.Grid.MeasureOverride(Size constraint)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutManager.Measure(ILayoutable control)
   at Avalonia.Layout.LayoutManager.ExecuteMeasurePass()
   at Avalonia.Layout.LayoutManager.InnerLayoutPass()
   at Avalonia.Layout.LayoutManager.ExecuteLayoutPass()
   at Avalonia.Threading.JobRunner.Job.Avalonia.Threading.JobRunner.IJob.Run()
   at Avalonia.Threading.JobRunner.RunJobs(Nullable`1 priority)
   at Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode)
@amwx
Copy link
Contributor

amwx commented Jul 11, 2020

From what I can tell this only happens if you change this after startup, as the example with the year hidden in the control catalog runs perfectly fine. It also applies to removing the last column of the grid (

The textblock columns are arranged based on the Day/Month/Year Visible properties & user date settings, but the spacers are hard coded to show a grid column 1 & 3
Grid.SetColumn(_spacer1, 1);
Grid.SetColumn(_spacer2, 3);

In this case, it then fails on line 1012 in Grid.cs, because PrivateCells[i].ColumnIndex is 3, which exceeds the number of columns in the Grid (3)
DefinitionsU[PrivateCells[i].ColumnIndex].UpdateMinSize(Math.Min(children[i].DesiredSize.Width, DefinitionsU[PrivateCells[i].ColumnIndex].UserMaxSize));

As a workaround, setting the spacer columns based on whether they should show does fix the issue:
Grid.SetColumn(_spacer1, columnIndex > 1 ? 1 : 0);
Grid.SetColumn(_spacer2, columnIndex > 2 ? 3 : 0);

but I'm not too familiar with the Grid code to know if this is correct behavior. I also don't know why this only fails if changing this during runtime

Update:
Can confirm this is not specific to DatePicker, but happens to any Grid where you reduce the number of columns, but leave controls with a Column index greater than the number of columns in the Grid.

@FoggyFinder
Copy link
Contributor Author

Can confirm this is not specific to DatePicker, but happens to any Grid where you reduce the number of columns, but leave controls with a Column index greater than the number of columns in the Grid.

Yep. As I said I saw it in my apps even when there was no such control as DatePicker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants