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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pcfist
Copy link

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

4 participants