Skip to content

Fix DataTable crash on resize when columns don't fit#599

Merged
michael-hawker merged 3 commits intoCommunityToolkit:mainfrom
pcfist:datatable-measureoverride-crash-fix
Feb 21, 2025
Merged

Fix DataTable crash on resize when columns don't fit#599
michael-hawker merged 3 commits intoCommunityToolkit:mainfrom
pcfist:datatable-measureoverride-crash-fix

Conversation

@pcfist
Copy link
Contributor

@pcfist pcfist commented Nov 17, 2024

DataTable sometimes crashes on resize, typically when it gets narrow enough for its columns not to fit. Crash happens in MeasureOverride because of negative value passed to Size ctor.

The easiest way to reproduce is to set DataTable's HorizontalAlignment to Left to disable "stretching" behavior. The following code snippet may be used (slightly modified version of DataTableSample.xaml):

<ListView ItemsSource="{x:Bind InventoryItems}" HorizontalAlignment="Left">
    <ListView.Header>
        <controls:DataTable Margin="12,0,0,0" HorizontalAlignment="Left">
            <controls:DataColumn Content="Id" />
            <controls:DataColumn CanResize="True" Content="Name" />
            <!--  Each column can be text or quickly customized by containing any content or restyled of course  -->
            <controls:DataColumn>
                <TextBlock FontWeight="SemiBold" Text="Description" />
            </controls:DataColumn>
            <controls:DataColumn Content="Quantity"/>
        </controls:DataTable>
    </ListView.Header>
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:InventoryItem">
            <controls:DataRow HorizontalAlignment="Left">
                <TextBlock VerticalAlignment="Center" Text="{x:Bind Id}" />
                <TextBlock VerticalAlignment="Center" Text="{x:Bind Name}" />
                <TextBlock VerticalAlignment="Center" Text="{x:Bind Description}" />
                <TextBlock VerticalAlignment="Center" Text="{x:Bind Quantity}" />
            </controls:DataRow>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style BasedOn="{StaticResource DefaultListViewItemStyle}"
                TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

This PR adds a check to make sure width value used in MeasureOverride() are non-negative.

@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 08c2fbf to 3eadb5a Compare November 19, 2024 00:02
@Arlodotexe Arlodotexe self-requested a review November 26, 2024 23:38
@Arlodotexe Arlodotexe enabled auto-merge November 26, 2024 23:39
auto-merge was automatically disabled November 27, 2024 09:52

Head branch was pushed to by a user without write access

@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 1c52238 to 6cc6525 Compare November 27, 2024 09:52
@pcfist pcfist requested a review from Arlodotexe November 27, 2024 15:39
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 6cc6525 to dc8741a Compare November 29, 2024 23:18
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 0d73135 to 65d4c84 Compare December 10, 2024 23:02
@michael-hawker michael-hawker enabled auto-merge (squash) December 11, 2024 18:17
auto-merge was automatically disabled December 25, 2024 16:40

Head branch was pushed to by a user without write access

@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 65d4c84 to 075b815 Compare December 25, 2024 16:40
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch 2 times, most recently from 11f292a to 6ac81c4 Compare January 8, 2025 23:21
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 6ac81c4 to e375d9a Compare January 25, 2025 23:36
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from e375d9a to 6aab97d Compare February 3, 2025 16:10
pcfist and others added 2 commits February 9, 2025 02:30
Suggested in code review, see !599

Co-authored-by: Andrii Chebukin <XperiAndri@Outlook.com>
@pcfist pcfist force-pushed the datatable-measureoverride-crash-fix branch from 6aab97d to e89df6e Compare February 8, 2025 23:30
@pcfist
Copy link
Contributor Author

pcfist commented Feb 20, 2025

@Arlodotexe, @michael-hawker, @xperiandri Could you please take a look at this MR when you have time? It's been reviewed a while ago, but seems to be stuck because of missing pipeline checks.

@michael-hawker
Copy link
Member

@pcfist sorry about that, GitHub holds things for new contributors. FYI @Arlodotexe. I've checked this locally. Saw it again and thought I remember seeing a fix before. We'll get this merged today. Thanks for the contribution! 🦙❤️

@michael-hawker michael-hawker merged commit f664523 into CommunityToolkit:main Feb 21, 2025
20 of 21 checks passed
@hoshiizumiya
Copy link

When will the DataTable be avaliable? The bug is still there because it's in the lab for a old version. @michael-hawker @Arlodotexe

@michael-hawker
Copy link
Member

@hoshiizumiya Labs packages are available on NuGet now, so you should be able to grab it from there, the packages are relatively new so they should contain this fix.

@hoshiizumiya
Copy link

hoshiizumiya commented Jan 8, 2026

@michael-hawker It has no update since 2023. Wait a minute, Why haven't I detected any updates? I just noticed that I need to check on prerelease box. Thank you anyway. But the crash bug wasn't fixed😕

@michael-hawker
Copy link
Member

@hoshiizumiya ah yes, pre-release checkbox is a must. Surprised there's any packages showing up otherwise on the feed as they should go stale and fall off, maybe an old local cache?

Anyway, if there's still an issue, please file a new bug with reproduction steps, could be a slightly different scenario than this one. Thanks!

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

DataTable sample #3 crashes when resizing the "Description" column beyond the "Quantity" column

5 participants