如何在列表视图xamarin表单中仅添加一次按钮

时间:2018-06-23 02:45:48

标签: xaml xamarin xamarin.forms

使用此代码

x:Class="Fit_Plans.TimelinePage"
Title="Timeline"
BackgroundColor="{ DynamicResource MainWrapperBackgroundColor }  
<ContentPage.Content>
    <ListView
        ItemsSource="{ Binding TimelineList }" 
        SeparatorVisibility="None"
        BackgroundColor="{ DynamicResource BasePageColor }"
        HasUnevenRows="true">
        <ListView.ItemTemplate>
            <DataTemplate>
               <ViewCell>
                    <ViewCell.View>
                        <StackLayout>
                            <StackLayout>
                                <Grid x:Name="GridTasks">
                                    <local:TimelineItemTemplate  
                                    Padding="{ DynamicResource MainWrapperPadding }"/>
                                </Grid>
                            </StackLayout>

                            <artina:Button   
                            Clicked="OnNextButtonTapped" 
                            Style="{DynamicResource PrimaryActionButtonStyle}" 
                            Text="Order" 
                            VerticalOptions="EndAndExpand"
                            WidthRequest="{ artina:OnOrientationDouble
                            LandscapeTablet=600 }"
                            HorizontalOptions="{ artina:OnOrientationLayoutOptions
                            PortraitPhone=Fill,
                            LandscapePhone=Fill,
                             PortraitTablet=Fill,
                            LandscapeTablet=Center }"/>

                            <StackLayout>

                            </StackLayout>
                        </StackLayout>

                    </ViewCell.View>
                </ViewCell>

            </DataTemplate>

        </ListView.ItemTemplate>

    </ListView>
</ContentPage.Content>

结果:

enter image description here

但是,如果我只想在列表视图的末尾仅显示一次Button按钮,如何实现呢?

我尝试了这个但没有成功:

<ContentPage.Content>


    <ListView
        ItemsSource="{ Binding TimelineList }" 
        SeparatorVisibility="None"
        BackgroundColor="{ DynamicResource BasePageColor }"
        HasUnevenRows="true">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                   <ViewCell.View>
                       <StackLayout>
                            <StackLayout>
                                <Grid x:Name="GridTasks">
                                    <local:TimelineItemTemplate  
                                    Padding="{ DynamicResource MainWrapperPadding }"/>
                                </Grid>
                            </StackLayout>
                            <StackLayout>
                           </StackLayout>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>
<artina:Button   
    Clicked="OnNextButtonTapped" 
    Style="{DynamicResource PrimaryActionButtonStyle}" 
    Text="Order" 
    VerticalOptions="EndAndExpand"
    WidthRequest="{ artina:OnOrientationDouble
                            LandscapeTablet=600 }"
    HorizontalOptions="{ artina:OnOrientationLayoutOptions
                            PortraitPhone=Fill,
                            LandscapePhone=Fill,
                             PortraitTablet=Fill,
                            LandscapeTablet=Center }"/>

它在最后显示了按钮,但有一个空列表

2 个答案:

答案 0 :(得分:3)

ListView提供HeaderTemplateFooterTemplate。您应该将按钮放在FooterTemplate内,如下所示:

<ListView ItemsSource="{ Binding TimelineList }" SeparatorVisibility="None" BackgroundColor="{ DynamicResource BasePageColor }" HasUnevenRows="true">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                      .................
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.FooterTemplate>
        <DataTemplate>
            <ContentView>
              //Put your button here
            </ContentView>  
         </DataTemplate>  
    </ListView.FooterTemplate>
</ListView>

答案 1 :(得分:0)

从上一个答案开始,您可以在页脚中添加内容,或者如果您希望将其按钮保留在单元格模板中,然后使用IsVisible =您的自定义转换器包装按钮,该代码将检查if (myIndex == totalListCount),然后按钮将仅显示列表中的最后一项。