【问题标题】:How to add a separator space between rows on custom Xamarin.Forms ViewCell?如何在自定义 Xamarin.Forms ViewCell 上的行之间添加分隔符空间?
【发布时间】:2015-08-28 17:08:24
【问题描述】:

this question on Xamarin Forums, Craig Dunn 教授如何创建带框架的单元格。

我想在每个单元格之间添加一个空格。

目前单元格似乎是粘在一起的,ViewCell 没有空格属性。

如何在自定义 Xamarin.Forms ViewCell 上的行之间添加分隔空间?

【问题讨论】:

  • 请将相关代码直接添加到问题中,以便人们确切知道问题所在。谢谢!

标签: c# listview grid xamarin.forms custom-cell


【解决方案1】:

您只需进一步自定义MenuCell 的布局即可实现此目的。

下面显示的是一个版本,它使用进一步的Xamarin.Forms.Frame 在每个项目之间创建一个间距,并进行了一些其他修改:-

XAML 页面:-

<ListView x:Name="lstItems" />

XAML 代码隐藏:-

lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell));
lstItems.ItemsSource = new string[] { "Apples", "Bananas", "Pears", "Oranges" };

ViewCell 类:-

public class MenuCell : ViewCell
{
    public MenuCell()
    {
        Label objLabel = new Label
        {
            YAlign = TextAlignment.Center,
            TextColor = Color.Yellow,                
        };
        objLabel.SetBinding(Label.TextProperty, new Binding("."));


        StackLayout objLayout = new StackLayout
        {
            Padding = new Thickness(20, 0, 0, 0),
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Children = { objLabel }
        };

        Frame objFrame_Inner = new Frame
        {
            Padding = new Thickness(15, 15, 15, 15),
            HeightRequest = 36,
            OutlineColor = Color.Accent,
            BackgroundColor = Color.Blue,
            Content = objLayout,                
        };

        Frame objFrame_Outer = new Frame
        {
            Padding = new Thickness(0, 0, 0, 10),
            Content = objFrame_Inner
        };

        View = objFrame_Outer;            
    }
}

将导致以下结果:-

【讨论】:

  • 基本上你需要填充一个包含元素。 &lt;ViewCell&gt;&lt;StackLayout Padding="10"&gt;&lt;MyNestedRowLayout /&gt;&lt;/StackLayout&gt;&lt;/ViewCell&gt;
【解决方案2】:

我的 xaml 示例:

 <ListView BackgroundColor="Gray" SeparatorVisibility="None" ItemsSource="{Binding Shipments}" x:Name="lstShipments" RowHeight="60">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="0,0,0,1">
            <Grid VerticalOptions="Fill" BackgroundColor="White">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="60"></ColumnDefinition>
              </Grid.ColumnDefinitions>
              <Label Grid.Column="0" Grid.Row="0" Text="{Binding DestinationCountry}" FontSize="16" />
              <Image Grid.Column="1" Grid.Row="0" Source="box32.png" />
              <Label Grid.Column="0" Grid.Row="1" Text="{Binding ExchangeOfficeDestinationTitle}" FontSize="16" />
              <Label Grid.Column="1" Grid.Row="1" Text="{Binding ShipmentNum}" FontSize="10" />

            </Grid>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

【讨论】:

  • 这是您卡片列表视图的完整 xaml 代码吗?我正在尝试您的 xaml 代码,但与您的屏幕截图没有任何关系
猜你喜欢
  • 2015-12-08
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2012-10-14
  • 2016-01-21
相关资源
最近更新 更多