【问题标题】:Item Templete for gridview in Xamarin Forms?Xamarin Forms中gridview的项目模板?
【发布时间】:2018-08-13 11:28:36
【问题描述】:

我想在 gridview 中使用一个项目模板,这样所有的孩子看起来都一样,这可能吗?或者在 Xamarin Forms 中是否有任何解决方法。

这是我的项目模板。

               <FlexLayout
                    Grid.Row="0" Grid.Column="0"
                    HorizontalOptions="CenterAndExpand"
                    VerticalOptions="CenterAndExpand"
                    Direction="Column" AlignContent="Center"
                    MinimumHeightRequest="110"
                    WidthRequest="90"
                    >
                    <Button BorderColor="#fff"
                        BorderWidth="1"
                        BackgroundColor="#4D000000"
                        HeightRequest="90" CornerRadius="45"
                        Image="vera"/>

                    <Label Text="Chat" 
                        Margin="0,4,0,0"
                        HeightRequest="20"
                        FontSize="9" 
                        HorizontalTextAlignment="Center" 
                        HorizontalOptions="CenterAndExpand"/>

                </FlexLayout>

【问题讨论】:

    标签: xaml xamarin gridview xamarin.forms itemtemplate


    【解决方案1】:

    您只需要在 Grid 的定义中添加布局。现在,如果您想要动态内容,您只需要绑定所需的属性。 这是不正确的,因为没有行等的定义,但您只需要在后面的代码中动态定义您想要的内容并相应地添加行和列

    <Grid>
        <Grid.RowDefinitions>
            //"Here you define your rows"
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            //"Here you define your columns"
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        //"Here you define your desired template"
        <FlexLayout                    
                    HorizontalOptions="CenterAndExpand"
                    VerticalOptions="CenterAndExpand"
                    Direction="Column" AlignContent="Center"
                    MinimumHeightRequest="110"
                    WidthRequest="90"
                    >
                    <Button BorderColor="#fff"
                        BorderWidth="1"
                        BackgroundColor="#4D000000"
                        HeightRequest="90" CornerRadius="45"
                        Image="vera"/>
    
                    <Label Text="Chat" 
                        Margin="0,4,0,0"
                        HeightRequest="20"
                        FontSize="9" 
                        HorizontalTextAlignment="Center" 
                        HorizontalOptions="CenterAndExpand"/>
    
         </FlexLayout>
    </Grid>
    

    【讨论】:

      【解决方案2】:

      为什么不使用 Listview?

      <ListView Grid.Row="0" Grid.Column="0" ItemSource="{Binding ListOfItem}">
      <ListView.ItemTemplate>
      <DateTemplate>
      <ViewCell>
      <FlexLayout   
                          HorizontalOptions="CenterAndExpand"
                          VerticalOptions="CenterAndExpand"
                          Direction="Column" AlignContent="Center"
                          MinimumHeightRequest="110"
                          WidthRequest="90">
                          <Button BorderColor="#fff" Command="{Binding ButtonCommand}"
                              BorderWidth="1"
                              BackgroundColor="#4D000000"
                              HeightRequest="90" CornerRadius="45"
                              Image="vera"/>
      
                          <Label Text="{Binding Text}" 
                              Margin="0,4,0,0"
                              HeightRequest="20"
                              FontSize="9" 
                              HorizontalTextAlignment="Center" 
                              HorizontalOptions="CenterAndExpand"/>
      </FlexLayout>
      <ViewCell>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>
      

      作为 ItemSource 创建一个对象,其中包含按钮的 Command 属性和 Name 的 String,并创建这些对象的列表。

      【讨论】:

        猜你喜欢
        • 2011-02-19
        • 1970-01-01
        • 2018-01-02
        • 2018-08-10
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多