dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Image in CollectionView DataTemplate does not respect WidthRequest under some conditions. #13710

Open drasticactions opened 1 year ago

drasticactions commented 1 year ago

Description

2023-03-06_12 53 50 2023-03-06_12 54 57 Grid-Image-Width-WinUI Grid-Image-Width-Android

For WinUI, Catalyst, and iOS, If you use an Image as a root element in a CollectionView and set its WidthRequest, the width for the image is set, but the underlying frame seems not to be, taking up the entire width of the container. On Android, it respects the set width.

I'm unsure if this is a bug or how images generally work on the platform. I can replicate this behavior by using

        <CollectionView ItemsSource="{Binding ImageLinks}" Grid.Row="0">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid  HeightRequest="200" WidthRequest="200">
                        <Image 
                            Source="dotnet_bot.png"
                            Background="Green"
                            SemanticProperties.Description="Cute dot net bot waving hi to you!" />
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>

Where the underlying image breaks out of the grid, even though the grid is set to have a width and height. However, if I use

            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image 
                            Source="dotnet_bot.png"
                            Background="Green"
                            SemanticProperties.Description="Cute dot net bot waving hi to you!"
                            HeightRequest="200"
                            WidthRequest="200" />
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>

Then the width and height are respected on all platforms.

Steps to Reproduce

  1. Checkout https://github.com/drasticactions/MauiRepros/tree/main/MauiImageLayout
  2. Run sample.

Link to public reproduction project repository

https://github.com/drasticactions/MauiRepros/tree/main/MauiImageLayout

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Windows, macOS

Affected platform versions

Current Stable.

Did you find any workaround?

Wrap the image in a Grid and then set the Width and Height on the image itself.

Relevant log output

No response

drasticactions commented 1 year ago
スクリーンショット 2023-03-06 15 37 58

This can also be replicated even with set widths inside of the grid.

Edit: Scratch that, I'm setting a color on a grid that is the full width, it better cover the whole thing! lol.

But the above should stand.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Zhanglirong-Winnie commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Can repro on windows and iOS platform with sample project. https://github.com/drasticactions/MauiRepros/tree/main/MauiImageLayout Screenshot 2023-06-25 173355 Screenshot 2023-06-25 173558