question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ItemsRepeater applying weird arranging behaviour to content when parent's ScrollViewer "HorizontalScrollBarVisibility" is set to "Visible".

See original GitHub issue

Describe the bug

When you create an ItemsRepeater and fill the content with differently width sized elements, the alignement of those elements is completely messed up, when you place a ScrollViewer as the parent of the ItemsRepeater and set the ScrollViewers HorizontalScrollBarVisibility to “Visible” or “Hidden” or “Auto” (Everything but “Disabled”).

Setting HorizontalAlignment of the Rectangles to “Center” does not help.

Steps to reproduce the bug

  1. Create a new UWP Solution
  2. Install Microsoft.UI.Xaml via Nuget
  3. In App.xaml replace with the following code:
<Application
    x:Class="ItemsRepeaterFix.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ItemsRepeaterFix">
    <Application.Resources>
        <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
    </Application.Resources>
</Application>
  1. In MainPage.xaml replace with the following code:
<Page
    x:Class="ItemsRepeaterFix.MainPage"
    x:Name="PageElement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ItemsRepeaterFix"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <muxc:ItemsRepeaterScrollHost>
            <ScrollViewer ZoomMode="Enabled" HorizontalScrollBarVisibility="Auto">
                <muxc:ItemsRepeater ItemsSource="{Binding ElementName=PageElement, Path=WidhtList}" HorizontalAlignment="Center">
                    <muxc:ItemsRepeater.Layout>
                        <muxc:StackLayout Orientation="Vertical" Spacing="15" ></muxc:StackLayout>
                    </muxc:ItemsRepeater.Layout>

                    <muxc:ItemsRepeater.ItemTemplate>
                        <DataTemplate x:DataType="x:Double">
                            <Rectangle Fill="Green" Width="{Binding}" Height="300"></Rectangle>
                        </DataTemplate>
                    </muxc:ItemsRepeater.ItemTemplate>
                </muxc:ItemsRepeater>
            </ScrollViewer>
        </muxc:ItemsRepeaterScrollHost>
    </Grid>
</Page>
  1. In MainPage.xaml.cs replace with the following code:
public sealed partial class MainPage : Page {
        public List<double> WidhtList { get; set; } = new List<double>() {
            500,
            500,
            500,
            1000,
            500,
            500,
            500,
        };
        public MainPage()
        {
            this.InitializeComponent();
        }
}

Expected behavior

All the rectangles are always aligned to the center of the screen and do not jump around.

Screenshots

image

NuGet package version

Microsoft.UI.Xaml 2.7.0

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

May 2021 Update (19043)

Additional context

If this is not a bug, I would incredibly appreciate a short guide on how to fix this issue.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kmahonecommented, Jul 8, 2022

@ranjeshj as FYI.

The layout is virtualizing, so as we realize more items with different sizes, the repeater’s width is changing, so we try to center based on what we realized so far. I don’t know if there is any way to know the maximum width without realizing everything.

Would a non-virtualizing layout work for your scenario? If so, you could try copy NonVirtualStackLayout code into you app from here: dev\Repeater\APITests\Common\NonVirtualStackLayout.cs

Alternatively, would it be possible for you to determine the max width ahead of time and set that as a width on the ItemsRepeater?

0reactions
DilanBoskancommented, Jul 25, 2022

@kmahone Alright thank you for the comment on the issue.

First of all, I do need the virtualization as my items are quite resource intensive. However, in my situation, I do know the maximum width of the elements in the ItemsRepeater so I was able to solve the issue based on your suggestion to set the width of the ItemsRepeater to the maximum width of the element in the collection, thank you.

I wonder how this would be solved if the maximum width of the elements where not known, say when loading images for example. The ItemsRepeater is obviously not supposed to behave as it does currently in that situation. Something is going wrong with the arranging of the elements.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ItemsRepeater - Windows apps
ItemsRepeater is a light-weight control to generate and present a collection of items.
Read more >
Parent control ScrollViewer scrolling instead of child ...
This property applies only to visible lines, and does not constrain the actual number of lines. Depending on its configuration, a text box...
Read more >
Implementing Scrolling in Windows using WPF ...
The Visible value makes sure the scrollbars are visible all the time regardless of the content needed scrolling or not.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found