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

ArgumentOutOfRangeException when selecting a DataGrid item, after changing DataGrid.ItemsSource #16164

Open
jwl4 opened this issue Jun 29, 2024 · 1 comment

Comments

@jwl4
Copy link

jwl4 commented Jun 29, 2024

Describe the bug

If a DataGrid.ItemsSource property is assigned in a LostFocus event, clicking an item in the DataGrid will throw ArgumentOutOfRangeException

To Reproduce

    <DockPanel>
        <Panel DockPanel.Dock="Top">
            <Border Margin="20">
                <TextBox 
                    Text="Click this textbox, then select an item in the list"
                    x:Name="Text" 
                    LostFocus="Text_LostFocus"
                    Width="350"
                    Padding="10" />
            </Border>
        </Panel>
        <DataGrid
            x:Name="Grid"
            AutoGenerateColumns="True"
            IsReadOnly="True">
        </DataGrid>
    </DockPanel>

using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Interactivity;

public partial class CrashyWindow : Window
{
    
    public CrashyWindow()
    {
        InitializeComponent();
        Grid.ItemsSource = MakeAList();
    }

    public void Text_LostFocus(object sender, RoutedEventArgs args)
    {
        // setting a grid's ItemsSource property in a LostFocus event will cause the grid
        //   to throw a System.ArgumentOutOfRangeException when an item in the grid is clicked
        // (tab doesn't cause a problem, nor does clicking in the grid's header row)
        Grid.ItemsSource = MakeAList();
    }

    private static List<string> MakeAList()
    {
        return ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"];
    }

}

Expected behavior

Maybe the DataGrid ought to process the changes in ItemsSource before passing the MouseDown to the DataGridCell?

Avalonia version

11.0.11

OS

Linux

Additional context

I tried using ObservableCollection instead of List, per the example at , but that gave the same exception.

I was attempting to parse a date in a textbox LostFocus event, then update the DataGrid using that date as a parameter. It's entirely possible that there's a better way to accomplish this.

@jwl4 jwl4 added the bug label Jun 29, 2024
@timunie
Copy link
Contributor

timunie commented Jul 1, 2024

I feel like AutoGenerateColumns may be the issue here.

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

No branches or pull requests

3 participants