2017-06-20 49 views
0

我是Xamarin Forms中的新成員。所以如果這是個愚蠢的問題,請原諒我。如何在Xamarin Forms中刪除ListView中的Extra Separator

我創建了簡單的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() 
     { 
     } 
    } 
} 

任何幫助是一個ppreciated。

回答

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> 
+0

不知道ListView.Footer解決了問題thnx。額外(非必要)行不再顯示。 – broadband

0

你也可以通過添加一個空字符串到頁腳屬性中刪除空單元格。

<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> 
相關問題