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

DataGrid Exception when bound to CollectionView and IsReadOnly=False #4929

Open
3 of 14 tasks
HoloMetrix opened this issue Aug 24, 2023 · 1 comment
Open
3 of 14 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control

Comments

@HoloMetrix
Copy link

HoloMetrix commented Aug 24, 2023

Describe the bug

I have a DataGrid with ItemSource bound to a grouped collection view. Adding a group to the bound collection fails if the DatGrid property ReadOnly = False. All works fine when IsReadOnly = True.

<Page
    x:Class="WinUI3BugDemonstrator.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
    xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:WinUI3BugDemonstrator"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:WinUI3BugDemonstrator.ViewModels"
    d:DataContext="{d:DesignInstance Type=vm:MainPageViewModel}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">
    <Page.Resources>
        <CollectionViewSource
            x:Name="CollectionView"
            IsSourceGrouped="True"
            Source="{Binding FilteredCollection, Mode=OneWay}" />
    </Page.Resources>
    <StackPanel>
        <Button x:Name="GenerateDataButton" Click="GenerateDataButton_Click">
            Make data and filter it!
        </Button>
        <controls:DataGrid
            x:Name="datagrid"
            Grid.Row="2"
            Margin="12"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            AutoGenerateColumns="False"
            IsReadOnly="False" <!-- changing this to true makes it all work fine -->
            ItemsSource="{x:Bind CollectionView.View, Mode=OneWay}">
            <controls:DataGrid.Columns>
                <controls:DataGridTemplateColumn Header="Value X">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Margin="4" Text="{Binding X}" />
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
                <controls:DataGridTemplateColumn Header="Value Y">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Margin="4" Text="{Binding Y}" />
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
            </controls:DataGrid.Columns>
        </controls:DataGrid>
    </StackPanel>
</Page>

Button click calls GenerateData in the view model.
Generating the data and filtering it causes an exception:

public partial class MainPageViewModel : ObservableObject
{
    private ObservableGroupedCollection<int, Vector2> rawCollection;

    [ObservableProperty]
    private ObservableGroupedCollection<int, MyData> filteredCollection;

    public void GenerateData()
    {
        rawCollection = new ObservableGroupedCollection<int, Vector2>();
        // Generate 10 data groups
        for (int i = 0; i < 10; i++)
        {
            List<Vector2> groupValues = new List<Vector2>();
            for (int j = 0; j < 5; j++)
            {
                groupValues.Add(new Vector2(Random.Shared.Next(100), Random.Shared.Next(100)));
            }
            var orderedList = groupValues.OrderBy(vector => vector.X);
            rawCollection.AddGroup(i, orderedList);
        }

        FilterData();
    }

    public void FilterData()
    {
        int maxXVal = 75;
        int maxYVal = 80;

        FilteredCollection = new ObservableGroupedCollection<int, MyData>();

        foreach (var group in  rawCollection)
        {
            List<MyData> currentGroup = new List<MyData>();
            foreach (var v in group)
            {
                if (v.X <= maxXVal && v.Y <= maxYVal)
                {
                    currentGroup.Add(new MyData { X = v.X.ToString(), Y = v.Y.ToString()});
                }
            }

            FilteredCollection.AddGroup(group.Key, currentGroup); // Null Reference exception here
        }

        OnPropertyChanged(nameof(this.FilteredCollection));
    }
}

MyData.cs only contains two string properties: X, Y.

Regression

No response

Reproducible in sample app?

  • This bug can be reproduced in the sample app.

Steps to reproduce

Using the minimal code example provided, set the IsReadOnly property of DataGrid to False. Click the button. See the Exception.

Expected behavior

Setting IsReadOnly = False should not cause an exception when changing the bound collection.

Screenshots

No response

Windows Build Number

  • Windows 10 1809 (Build 17763)
  • Windows 10 1903 (Build 18362)
  • Windows 10 1909 (Build 18363)
  • Windows 10 2004 (Build 19041)
  • Windows 10 20H2 (Build 19042)
  • Windows 10 21H1 (Build 19043)
  • Windows 11 21H2 (Build 22000)
  • Other (specify)

Other Windows Build number

Windows 10 22H2 (Build 19045.3324)

App minimum and target SDK version

  • Windows 10, version 1809 (Build 17763)
  • Windows 10, version 1903 (Build 18362)
  • Windows 10, version 1909 (Build 18363)
  • Windows 10, version 2004 (Build 19041)
  • Other (specify)

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

17.7.1

Device form factor

Desktop

Nuget packages

  • Community.Toolkit.Mvvm (8.2.1)
  • Community.Toolkit.WinUI (7.1.2)
  • Community.Toolkit.WinUI.UI.Controls (7.1.2)
  • Microsoft.Windows.SDK.BuildTools (10.0.22621.756)
  • Microsoft.WindowsAppSDK (1.3.230724000)

Additional context

No response

Help us help you

Yes, but only if others can assist.

@HoloMetrix HoloMetrix added the bug 🐛 An unexpected issue that highlights incorrect behavior label Aug 24, 2023
@ghost ghost added the needs triage 🔍 label Aug 24, 2023
@ghost
Copy link

ghost commented Aug 24, 2023

Hello HoloMetrix, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

@HoloMetrix HoloMetrix changed the title DataGrid Exception when bound to CollectionView and ReadOnly=False DataGrid Exception when bound to CollectionView and IsReadOnly=False Aug 24, 2023
@michael-hawker michael-hawker added DataGrid 🔠 Issues on DataGrid control and removed needs triage 🔍 labels Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control
Projects
None yet
Development

No branches or pull requests

2 participants