【问题标题】:How to add a button only once in a List View xamarin Forms如何在列表视图 xamarin Forms 中仅添加一次按钮
【发布时间】:2018-06-23 13:36:15
【问题描述】:

使用此代码

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>

结果:

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

我试过这个没有成功:

<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 }"/>

它在最后显示按钮,但列表为空

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    ListView 提供HeaderTemplate 和FooterTemplate。您应该将您的按钮放在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>
    

    【讨论】:

    • 使用&lt;ListView.Footer&gt; &lt;/ListView.Footer&gt; 解决了我的问题
    【解决方案2】:

    截至上一个答案,您可以在页脚中输入,或者如果您想将按钮保留在单元格模板中,然后使用 IsVisible = 您的自定义转换器包装按钮,这将检查 if (myIndex == totalListCount) 然后按钮将仅针对列表中的最后一项显示。

    【讨论】:

    • 尼克,但是,这可能会解决目的,但它不是实现此目的的好方法。这将为每个 ListView 项创建一个按钮实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多