How to measure the actual view size loaded in the DataTemplate in Xamarin Forms?

Dinesh4693Dinesh4693 INMember ✭✭
edited December 2018 in Xamarin.Forms

Hi Everyone,

I would like to measure the view (generic) loaded in the DataTemplate in Xamarin Forms. For example, if I load an Grid (with multiple children) inside the DataTemplate, I would like to know the actual size of the Grid to layout it in the view.

Code Example:

`var datatemplate = new DataTemplate(() =>
{
var grid = new Grid();
...
var nameLabel = new Label { FontAttributes = FontAttributes.Bold };
var ageLabel = new Label();
var locationLabel = new Label { HorizontalTextAlignment = TextAlignment.End };

            nameLabel.SetBinding(Label.TextProperty, "Name");
            ageLabel.SetBinding(Label.TextProperty, "Age");
            locationLabel.SetBinding(Label.TextProperty, "Location");

            grid.Children.Add(nameLabel);
            grid.Children.Add(ageLabel, 1, 0);
            grid.Children.Add(locationLabel, 2, 0);

            return new ViewCell { View = grid };

});

var content = (datatemplate.CreateContent() as ViewCell).View;

//Now, it will get the element loaded in the data template.
//How to get the actual size of this view.
var size = content;
`

I know that, it is possible in Xamarin Forms because the Xamarin.Forms ListView with HasUnevenRows=”true” property does the same, it measures the view loaded in the DataTemplate and layout it accordingly in the View.

So, can anyone please suggest me how it can be achieved similarly in application?

Many thanks in Advance !!!

Regards,
Dinesh

Sign In or Register to comment.