Maui ScrollView - Scrollbar length not updating when content dynamically changed

Viewed 222

I tried this content with TwoWay mode.
I used ObservableCollection, when I modify the content is updated, but the scroll length is not updated.

![scrollview content is too long

Right Scrollbar is too long.
How can I update the scrollbar length?

<ScrollView BackgroundColor="LightGray" x:Name="InfoStack">
        <StackLayout
            BindableLayout.ItemsSource="{Binding Source={x:Static local:InfoHelpers.Ints}, Mode=TwoWay}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <.../>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
2 Answers

After changing content, this might fix it:

(InfoStack as IView).InvalidateArrange();

HACK: If that doesn't do anything, try a slight delay:

Dispatcher.Dispatch(async() =>
{
    await Task.Delay(100);
    (InfoStack as IView).InvalidateArrange();
}

(While hacks that involve a time delay are not reliable, if the first one doesn't work, but this does, that might help someone identify the "real" problem.)

It behavior differently on different platforms .

I did test your code on Windows desktop , iOS and Android simulator .

The result is that it only works on Windows desktop, ScrollView does not expand dynamatically as expected on iOS and Android .

It seems like a potential issue , consider rainsing the issue on github :https://github.com/dotnet/maui/issues .

Workaround

Use CollectionView instead of ScrollView.

<CollectionView ItemsSource="{Binding Source={x:Static local:InfoHelpers.Ints}, Mode=TwoWay}">
   <CollectionView.ItemTemplate>
      <DataTemplate>      
          <Label Text="{Binding .}" HeightRequest="40"/>             
      </DataTemplate>
   </CollectionView.ItemTemplate>
</CollectionView>
Related