Xamarin.Forms:检测 BindableLayout 中的最后一项

Xamarin.Forms: Detect last item in a BindableLayout

我正在使用可绑定布局来显示项目列表。这些项目绑定到键值对的 ObservableRangeCollection

public ObservableRangeCollection<KeyValuePair<string, string>> Items { get; set; }

  

这是呈现项目的布局——每一行都使用 BoxView 由一行分隔:

<StackLayout BackgroundColor="Transparent" BindableLayout.ItemsSource="{Binding Items}" Padding="20">
    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="1" />
                                </Grid.RowDefinitions>

                                <Label Grid.Column="0"
                                       Grid.Row="0"
                                       Text="{Binding Key}"/>
                                
                                <Label Grid.Column="1"
                                       Grid.Row="0"
                                       HorizontalOptions="End"
                                       HorizontalTextAlignment="End"
                                       Text="{Binding Value}"/>
                                       
                                <BoxView Grid.Column="0"
                                         Grid.Row="1"
                                         Grid.ColumnSpan="2"
                                         HeightRequest="1" />
                            </Grid>
                        </DataTemplate>
   </BindableLayout.ItemTemplate>
 </StackLayout>

问题是在列表末尾有一个额外的行,我希望在呈现可观察集合中的最后一个元素后将其隐藏。

我需要弄清楚最后一个项目是否正在 BindableLayout 上呈现并将 BoxView 的 IsVisible 属性 设置为 false 但我不确定如何使用 XAML.

一种方法是使用布尔值 属性 定义 class,您将每个项目设置为 true 或 false。这控制是否显示 BoxView。

MyItem.cs:

public class MyItem
{
    public string Key { get; set; }
    public string Value { get; set; }
    /// <summary>
    /// Default to "true". Most items will show the line.
    /// </summary>
    public bool ShowLineUnder { get; set; } = true;

MyModelView.cs:

public class MyModelView
{
    public ObservableCollection<MyItem> Items { get; set; }

    public MyModelView()
    {
        Items.Add(new MyItem(...));
        Items.Add(new MyItem(...));
        Items.Add(new MyItem(...));

        // Hide line on last item.
        Items[Items.Count - 1].ShowLineUnder = false;
    }
}

在 ItemTemplate 的 XAML 中:

<BoxView ... IsVisible="{Binding ShowLineUnder}" />

根据需要进行调整。

另一种方法是将 BindingLayout 的每个成员视为布局的 Children 集合中的一个条目。有了这些知识,您只需隐藏 Children 集合中最后一个子项的 BoxView。

var layout = [reference to the StackLayout];
layout.Children.Last().IsVisible = false; // add using for System.Linq

除了使用 IsVisible 隐藏 BoxView 属性,您还可以通过获取对网格 ColumnDefinition 的引用并将 Width 设置为 0 来隐藏它。当存在多个控件时,这会很有用隐藏。