【问题标题】:Xamarin.Forms: Detect last item in a BindableLayoutXamarin.Forms:检测 BindableLayout 中的最后一项
【发布时间】:2021-12-05 03:17:36
【问题描述】:

我正在使用可绑定布局来显示项目列表。项目绑定到键值对的 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 执行此操作。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    一种方法是定义一个具有布尔属性的类,您将每个项目设置为真或假。这控制该 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}" />
    

    根据需要进行调整。

    【讨论】:

      【解决方案2】:

      另一种方法是认为 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 来隐藏它。这在要隐藏多个控件时很有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-07
        • 1970-01-01
        • 2017-02-10
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多