Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

CollectionView Span with Binding Not Working

$
0
0

I'm having a play around with the CollectionView and have encountered an issue where binding to the Span is not working. Basically - if the device is in portrait mode, I would like to see Span="2", and in Landscape mode Span="3", however, the binding doesn't seem to be working... Is this a bug? or is there another way to achieve my requirement? My VM's NumberOfColumns is changing correctly but is not reflected by the control.

            <CollectionView ItemsSource="{Binding Items}" Margin="6">
                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical" Span="{Binding NumberOfColumns}" HorizontalItemSpacing="6" VerticalItemSpacing="6"/>
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Frame CornerRadius="9" BackgroundColor="PaleGoldenrod"/>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>


        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);

            if (BindingContext is MainViewModel mv)
            {
                mv.LayoutMode = width < height
                ? LayoutMode.Portrait
                : LayoutMode.Landscape;
            }
        }


        public LayoutMode LayoutMode
        { 
            get
            {
                return layoutMode;
            }

            set
            {
                layoutMode = value;
                OnPropertyChanged();
                NumberOfColumns = layoutMode == LayoutMode.Portrait
                    ? 2
                    : 3;
            }
        }

        public int NumberOfColumns
        {
            get => numberOfColumns;
            set
            {
                numberOfColumns = value;
                OnPropertyChanged();
            }
        }

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>