Hello,
Welcome to our Microsoft Q&A platform!
Items in a CollectionView
can be dynamically resized by changing layout related properties of elements within the DataTemplate.
In your DataTemplate
, you just have Label
, if you want to change the Height
, you could set HeightRequest
for your Label
<Label HeightRequest="300" Text="{Binding .}" BackgroundColor="Green"/>
Here is related article about it.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/layout#dynamic-resizing-of-items
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
@njsokal
You can use Two Stacklayout, set the HeightRequest for your inner
StackLayout
, then you can add whatever you want, just calculate the necessary height for once.Note: Outside the stacklayout' defaulted height is your screen's height.Once again, that still requires me to manually calculate the height.
Can you share picture that you want to achieve? Due to your Qustion is: How can I make this be the height of the items in the DataTemplate?. If you do not want to specific height, you just want items' height adjust by himself, you do not change anything. Becuase it will dynamic resizing of items. If you have some dobuts about
VerticalOptions
, please read this article:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/layout-options@Leon Lu (Shanghai Wicresoft Co,.Ltd.) What we want to know is how to make CollectionView set its own height automatically. By default, CollectionView elements expand to fill all available vertical space in their parent element. I believe that the ListView element did the same thing.
What I expected CollectionView to do:
When the CollectionView contains three items that are 50px tall, I expected the CollectionView height to be 150px (3 items x 50px tall = 150px).
What CollectionView actually does:
With any number of 50px tall items (1-Infinite), the CollectionView element's height is equal to the total height of the page (greater if there is an element preceding it).
I have not figured this out yet. Right now, the only thing I can think of is using a converter in the CollectionView > HeightRequest property that receives the collection that is used as the ItemsSource, then returns an integer equal to the count * height of items. This solution feels gross though. It seems that we need to use StackLayout instead of CollectionView... StackLayout isn't a great alternative for me, though. I want a list of items that can be tapped, invoking a command upon tapping. CollectionView is better suited for this use case.
Sign in to comment