如何使BindableLayout可滚动

huangapple go评论52阅读模式
英文:

How to make the BindableLayout to be scrollable

问题

我有一个列表,我不想在其中使用ListView,因为我要如何使其可滚动,数据已绑定,但我无法滚动它。

<Grid Grid.Row="0" IsVisible="{Binding SearchListLayout}" Margin="0" Padding="0" BackgroundColor="White">
    <ScrollView>
        <StackLayout BindableLayout.ItemsSource="{Binding ProductsList}" x:Name="SearchLayoutItemList" HeightRequest="255">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Grid RowDefinitions="37,Auto">
                        <Image Grid.Row="0" Source="searchingicon.png" Style="{DynamicResource icons}"/>
                        <Label Grid.Row="0" Text="{Binding name}" FontSize="14" TextColor="Black" Margin="50,0,0,0" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                        <Image Grid.Row="0" Source="reload.png" Style="{DynamicResource icons}" HorizontalOptions="EndAndExpand" Margin="0,0,15,0"/>
                        <BoxView Grid.Row="1" Style="{DynamicResource DarkSeparator}" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</Grid>
英文:

I have one list and i don't want to use listview inside it as how can i make it scrollable, data is binding but i am not able to scroll it.

<Grid Grid.Row="0" IsVisible="{Binding SearchListLayout}" Margin="0" Padding="0" BackgroundColor="White">
    <ScrollView>
        <StackLayout BindableLayout.ItemsSource="{Binding ProductsList}" x:Name="SearchLayoutItemList" HeightRequest="255">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Grid RowDefinitions="37,Auto">
                        <Image Grid.Row="0" Source="searchingicon.png" Style="{DynamicResource icons}"/>
                        <Label Grid.Row="0" Text="{Binding name}" FontSize="14" TextColor="Black" Margin="50,0,0,0" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                        <Image Grid.Row="0" Source="reload.png" Style="{DynamicResource icons}" HorizontalOptions="EndAndExpand" Margin="0,0,15,0"/>
                        <BoxView Grid.Row="1" Style="{DynamicResource DarkSeparator}" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</Grid>

答案1

得分: 2

尽管BindableLayout在技术上可以通过StackLayout来显示您的项目列表,并且StackLayout的高度受到您的高度请求的限制;但包围的ScrollViewer没有可滚动的内容,因为它完全包含了您的StackLayout。

为了实现您可能想要的效果(一个固定的255像素的StackLayout,可以滚动您的项目),ScrollViewer应该在StackLayout内部作为呈现项目的ItemsPanel。同样,您可以通过自定义的BindableLayouts来实现这一点,但这恰恰是Xamarin Forms的ListView默认所做的。

您可能只需将您的BindableLayout实现更改为ListView:

<Grid Grid.Row="0" IsVisible="{Binding SearchListLayout}" Margin="0" Padding="0" BackgroundColor="White">
    <ListView ItemsSource="{Binding ProductsList}" x:Name="SearchLayoutItemList" HeightRequest="255">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid RowDefinitions="37,Auto">
                    <Image Grid.Row="0" Source="searchingicon.png" Style="{DynamicResource icons}"/>
                    <Label Grid.Row="0" Text="{Binding name}" FontSize="14" TextColor="Black" Margin="50,0,0,0" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                    <Image Grid.Row="0" Source="reload.png" Style="{DynamicResource icons}" HorizontalOptions="EndAndExpand" Margin="0,0,15,0"/>
                    <BoxView Grid.Row="1" Style="{DynamicResource DarkSeparator}" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
英文:

Although the BindableLayout technically can display your list of items via the StackLayout, and the StackLayout height is being restraint to your height request; the encompassing ScrollViewer has nothing to scroll since it fully contains your StackLayout.

To achieve what you probably want (a fixed 255 pixel StackLayout that scrolls your items) the ScrollViewer should be inside the StackLayout as the rendered ItemsPanel for you Items. Again, you could achieve this with custom BindableLayouts, but this is exactly what Xamarin Forms ListView does by default.

You probably simply want to change your BindableLayout implementation to a ListView:

&lt;Grid Grid.Row=&quot;0&quot; IsVisible=&quot;{Binding SearchListLayout}&quot; Margin=&quot;0&quot; Padding=&quot;0&quot; BackgroundColor=&quot;White&quot;&gt;
    &lt;ListView ItemsSource=&quot;{Binding ProductsList}&quot; x:Name=&quot;SearchLayoutItemList&quot; HeightRequest=&quot;255&quot;&gt;
        &lt;ListView.ItemTemplate&gt;
            &lt;DataTemplate&gt;
                &lt;Grid RowDefinitions=&quot;37,Auto&quot;&gt;
                    &lt;Image Grid.Row=&quot;0&quot; Source=&quot;searchingicon.png&quot; Style=&quot;{DynamicResource icons}&quot;/&gt;
                    &lt;Label Grid.Row=&quot;0&quot; Text=&quot;{Binding name}&quot; FontSize=&quot;14&quot; TextColor=&quot;Black&quot; Margin=&quot;50,0,0,0&quot; HorizontalOptions=&quot;StartAndExpand&quot; VerticalOptions=&quot;CenterAndExpand&quot;/&gt;
                    &lt;Image Grid.Row=&quot;0&quot; Source=&quot;reload.png&quot; Style=&quot;{DynamicResource icons}&quot; HorizontalOptions=&quot;EndAndExpand&quot; Margin=&quot;0,0,15,0&quot;/&gt;
                    &lt;BoxView Grid.Row=&quot;1&quot; Style=&quot;{DynamicResource DarkSeparator}&quot; /&gt;
                &lt;/Grid&gt;
            &lt;/DataTemplate&gt;
        &lt;/ListView.ItemTemplate&gt;
    &lt;/ListView&gt;
&lt;/Grid&gt;

答案2

得分: 0

ScrollView的高度请求设置为"255"会导致无法滚动,因为ScrollView中的StackLayout的高度与ScrollView的高度相等,左侧内容被覆盖。

当我移除StackLayout中的HeightRequest时,ScrollView可以滚动。但是ScrollView会占满整个页面,我认为这不是您想要的效果。因此,您可以在ScrollView外部添加一个父级StackLayout,将ScrollView的高度设置为255,并同时允许滚动。我只是提供了一个示例:

<StackLayout>
    <ScrollView HeightRequest="255">
        <StackLayout BindableLayout.ItemsSource="{Binding ItemCollection}" x:Name="SearchLayoutItemList">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Grid RowDefinitions="37,Auto">
                        <Image Grid.Row="0" Source="searchingicon.png" />
                        <Label Grid.Row="0" Text="{Binding name}" FontSize="14" TextColor="Black" Margin="50,0,0,0" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                        <Image Grid.Row="0" Source="reload.png"  HorizontalOptions="EndAndExpand" Margin="0,0,15,0"/>
                        <BoxView Grid.Row="1"  BackgroundColor="Yellow"/>
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</StackLayout>

欲了解更多信息,请参考Xamarin.Forms ScrollView

希望对您有所帮助。

英文:

The scrollview could not scroll as you set the HeightRequest="255" for the stacklayout in the scrollview. That because the display content's height is equal with the Scrollview, left content was covered.

Then I remove the HeightRequest
in StackLayout, the ScrollView could scroll. But the scrollview will fill the whole page which I thought is not what you want. So you could add parent layout Stacklayout outside the scrollview which set the size of scrollview equal to 255 and let it scroll at the same time. I just made an example :

&lt;StackLayout&gt;
    &lt;ScrollView HeightRequest=&quot;255&quot;&gt;
        &lt;StackLayout BindableLayout.ItemsSource=&quot;{Binding ItemCollection}&quot; x:Name=&quot;SearchLayoutItemList&quot; &gt;
            &lt;BindableLayout.ItemTemplate&gt;
                &lt;DataTemplate&gt;
                    &lt;Grid RowDefinitions=&quot;37,Auto&quot;&gt;
                        &lt;Image Grid.Row=&quot;0&quot; Source=&quot;&quot;searchingicon.png&quot;&quot; /&gt;
                        &lt;Label Grid.Row=&quot;0&quot; Text=&quot;{Binding name}&quot; FontSize=&quot;14&quot; TextColor=&quot;Black&quot; Margin=&quot;50,0,0,0&quot; HorizontalOptions=&quot;StartAndExpand&quot; VerticalOptions=&quot;CenterAndExpand&quot;/&gt;
                        &lt;Image Grid.Row=&quot;0&quot; Source=&quot;reload.png&quot;  HorizontalOptions=&quot;EndAndExpand&quot; Margin=&quot;0,0,15,0&quot;/&gt;
                        &lt;BoxView Grid.Row=&quot;1&quot;  BackgroundColor=&quot;Yellow&quot;/&gt;
                    &lt;/Grid&gt;
                &lt;/DataTemplate&gt;
            &lt;/BindableLayout.ItemTemplate&gt;
        &lt;/StackLayout&gt;
    &lt;/ScrollView&gt;
&lt;/StackLayout&gt;

For more info, you could refer to Xamarin.Forms ScrollView.

Hope it works for you.

huangapple
  • 本文由 发表于 2023年1月9日 19:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定