【问题标题】:How to remove Extra Separator in my ListView in Xamarin Forms如何在 Xamarin 表单中的 ListView 中删除额外的分隔符
【发布时间】:2017-06-20 10:54:16
【问题描述】:

我是 Xamarin 表单的新手。如果这是个愚蠢的问题,请原谅我。

我创建了简单的 ListView。但我想删除行有空的额外分隔符。

我尝试在 SO、google 和 Xamarin Forms 上进行搜索。但对我没有任何帮助。

Xaml 代码:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewDemo" x:Class="ListViewDemo.ListViewDemoPage">
    <StackLayout Orientation="Vertical"
             VerticalOptions="Fill"
             HorizontalOptions="StartAndExpand">
        <ListView x:Name="MainListView" Margin="0,30,0,0" VerticalOptions="FillAndExpand"  SeparatorColor="Red" BackgroundColor="Gray" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <Label Text="{Binding Name}" Grid.Column="0" Margin="10,10,0,0">
                            </Label>
                            <Label Text="{Binding Age}" Grid.Column="1" Margin="0,10,10,0">
                            </Label>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

ListViewDemoPage.cs

using System.Collections.Generic;
using Xamarin.Forms;

namespace ListViewDemo
{
    public partial class ListViewDemoPage : ContentPage
    {
        public ListViewDemoPage()
        {
            InitializeComponent();

            var strings = new List<EmptyClass>
            {
                new EmptyClass{Name = "Harshad Harshad Harshad Harshad Harshad Harshad Harshad Harshad", Age = 23},
                 new EmptyClass{Name = "Sunita", Age = 23},
                 new EmptyClass{Name = "Rahul", Age = 23},
                 new EmptyClass{Name = "Vrushbh", Age = 23},
                 new EmptyClass{Name = "Harmi", Age = 23},
                 new EmptyClass{Name = "Jigu", Age = 23},

            };

            MainListView.ItemsSource = strings; 
        }
    }
}

EmptyClass.cs

using System;
namespace ListViewDemo
{
    public class EmptyClass
    {
        public string Name { get; set; }
        public double Age { get; set; }
        public EmptyClass()
        {
        }
    }
}

感谢任何帮助。

【问题讨论】:

    标签: listview xamarin xamarin.forms separator


    【解决方案1】:

    只需在ListView.ItemTemplate下方添加页脚

    <ListView.Footer>
                <StackLayout Orientation="Horizontal">
                </StackLayout>
    </ListView.Footer>
    

    完整代码:

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewDemo" x:Class="ListViewDemo.ListViewDemoPage">
        <ListView x:Name="MainListView" Margin="0,30,0,0" VerticalOptions="FillAndExpand" SeparatorColor="Red" BackgroundColor="Gray" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <Label Text="{Binding Name}" Grid.Column="0" Margin="10,10,0,0">
                            </Label>
                            <Label Text="{Binding Age}" Grid.Column="1" Margin="0,10,10,0">
                            </Label>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Footer>
                <StackLayout Orientation="Horizontal">
                </StackLayout>
            </ListView.Footer>
        </ListView>
    </ContentPage>
    

    【讨论】:

    • 不知道 ListView.Footer 解决了这个问题,thnx。多余的(非必要的)行不再显示。
    【解决方案2】:

    您还可以通过在页脚属性中添加一个空字符串来删除空单元格。

    <ListView 
        x:Name="MainListView" 
        Margin="0,30,0,0" 
        VerticalOptions="FillAndExpand" 
        SeparatorColor="Red" 
        BackgroundColor="Gray"
        Footer=""
        HasUnevenRows="true">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                        ....
                  <!-- Removed for simplicity -->
                        ....
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.Footer>
            <StackLayout Orientation="Horizontal">
            </StackLayout>
        </ListView.Footer>
    </ListView>
    

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      相关资源
      最近更新 更多