Default padding for list view cell

MaximTkMaximTk USMember ✭✭

Hi all.
I'm trying to implement a custom list view cell with predefined padding. Basically I have a lot of lists and for each of them I need to write data template something like this:

     <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
              <StackLayout Padding="5">
                    <!-- my controls -->
              </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>

So I decided to write a custom cell:

    class MyCell : ViewCell
    {
        public MyCell()
        {
            View = new StackLayout { Padding=5 };
        }
    }

And I want to use it like this:

    <ListView.ItemTemplate>
      <DataTemplate>
        <c:MyCell>
          <Label Text="{Binding Title}" />
          <Label Text="{Binding Text}" />
        </c:MyCell>
      </DataTemplate>
    </ListView.ItemTemplate>

But in this case I have only last control in list view cell (<Label Text="{Binding Text}" /> from my example). I found that when I set a stacklayoutas a Viewfor MyCellthan OnChildAdded event fires and it tryes to add self to the View.
I tryed to handle this behavior but I have the same result with only last control in list view cell:

    protected override void OnChildAdded(Element child)
    {
        var content = View as StackLayout;
        var _child = child as StackLayout;

        if (content == _child)
            return;

        base.OnChildAdded(child);

        content?.Children.Add(child as View);
    }

So how I can achieve my goal?

Sign In or Register to comment.