Home > Mobile >  Carousal view with multiple items without databinding
Carousal view with multiple items without databinding

Time:09-15

How do I set static text as Carousal view items instead of doing data binding? Here in the below code I am trying to have Page 2 appear when the user swipes to the left. This code says The property ItemTemplate is set more than once

    <StackLayout Margin="10">
        <CarouselView>
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="Page 1"/>
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="Page 2"/>
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </StackLayout>

CodePudding user response:

If you want to set the items statically in xaml itself without data binding you can use Array markup extension of type string:

   <StackLayout Margin="10">
       <CarouselView>
            <CarouselView.ItemsSource>
                <x:Array Type="{x:Type sys:String}">
                    <sys:String>Page 1</sys:String>
                    <sys:String>Page 2</sys:String>
                </x:Array>
            </CarouselView.ItemsSource>

            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="{Binding .}" />
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </StackLayout>

PS: Don't forget to add the required xaml namespace:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
  • Related