Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Cells in StackLayout?

$
0
0

I'd like to wrap a collection of cells defined in XAML in a simple StackLayout. While the compiler allows this, at runtime it throws an InvalidCastException because Children is expecting a list of Views, and Cells don't derive from that. (And no, ViewCells don't derive from View either.)

I've written this custom control called StackView that attempts to do this, but sizing to the contents is still awkward.

[ContentProperty("Children")]
public class StackView : TableView
{
    TableSection section = new TableSection();

    public ObservableCollection<Cell> Children { get; } = new ObservableCollection<Cell>();

    public StackView()
    {
        Root.Add(section);
        Children.CollectionChanged += OnCollectionChanged;
        RowHeight = MenuCell.DefaultCellHeight;
    }

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Cell item in e.NewItems)
                section.Add(item);
        }
        if (e.OldItems != null)
        {
            foreach (Cell item in e.OldItems)
                section.Remove(item);
        }

        HeightRequest = section.Count * (RowHeight + 1);
    }
}

I then reference this in XAML to create a simple pop-up menu:

    <Frame HorizontalOptions="End" VerticalOptions="Start" CornerRadius="1" HasShadow="True" Padding="0">
      <c:StackView WidthRequest="240">
        <ImageCell Text="Sync" ImageSource="ic_sync.png" Command="{Binding RefreshCommand}" />
        <ImageCell Text="Remove" ImageSource="ic_remove.png" Command="{Binding RemoveCommand}" />
        <ImageCell Text="Info" ImageSource="ic_info.png" Command="{Binding DetailCommand}" />
      </c:StackView>
    </Frame>

There must be a more elegant way to do this, right?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>