Xamarin表格中用于Templeview的Templete项目?

时间:2018-08-13 11:28:37

标签: xaml xamarin gridview xamarin.forms itemtemplate

我想在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>

2 个答案:

答案 0 :(得分:0)

您只需要在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>

答案 1 :(得分:0)

您为什么不使用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属性和名称的String,并创建这些对象的列表。