首页 > 解决方案 > Xamarin:在代码隐藏中更改 GridItemLayout 的值

问题描述

 <CollectionView Grid.Row="2" Grid.Column="0" x:Name="collectionViewItemsLayout" ItemsSource="{Binding BaseCustomersCards}"  ItemTemplate="{StaticResource CustomerCardTemplateSelector}" >
                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical" Span="5" />
                </CollectionView.ItemsLayout>
            </CollectionView>

我有这个collectionview,但正在尝试更改手机平板电脑的跨度。默认值为 5,但电话值应为 3。

  var idiom = DeviceInfo.Idiom;
            if (idiom == DeviceIdiom.Phone)
            {
                collectionViewItemsLayout.SetValue(GridItemsLayout.SpanProperty, 3);
            }

我在代码隐藏中进行了更改以更改它,该方法触发但不会更改任何内容。我试图将 3 作为字符串和纯值。我也尝试将 x:name 放在属性中,但它不能去那里。

标签: c#xamlxamarinxamarin.forms

解决方案


通过创建一个新的 GridItem 并设置它来工作

 if (idiom == DeviceIdiom.Phone)
            {
                var grid = new GridItemsLayout(ItemsLayoutOrientation.Vertical)
                {
                    Span = 3,
                };

                collectionViewItemsLayout.SetValue(CollectionView.ItemsLayoutProperty, grid);
            }

推荐阅读