Adam Jachocki
Adam Jachocki

Reputation: 2125

Xamarin - get control dimensions

I really don't know how to get control dimensions in Xamarin Forms. I thought it would be as easy as:

View control = GetControlSomehow();
var bounds = control.Bounds;

But View.Bounds returns different things depending on wether the control is inside a grid or not; or wether the main layout is AbsoluteLayout or some other one.

For example if I do something like:

<AbsoluteLayout>
    <Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"/>
</AbsoluteLayout>

then I am not able to read actual height or width of that button. Although MSDN says that Height & Width properties should return these values.

I tried even:

var width = ctrl.Width;

or

var width = ctrl.GetValue(WidthProperty);

But really non of these worked. I always get -1

So how can I get the control dimensions that could be reliable?

Upvotes: 0

Views: 278

Answers (3)

Adam Jachocki
Adam Jachocki

Reputation: 2125

Sorry guys, my bad. I was testing it on two pages - the real one and the test one. The test page is built in a different way and I confirm that the problem was in time when I retrieved these values. Control wasn't painted yet.

So I am really sorry to bother you and thank you all for investing your time into this.

Upvotes: 0

ToolmakerSteve
ToolmakerSteve

Reputation: 21233

Complete example showing how to pass button itself, using data binding.

AbsoluteLayoutPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestBugs.AbsoluteLayoutPage">
    <ContentPage.Content>
        <AbsoluteLayout>
            <Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"
                    Command="{Binding ButtonCommand}" CommandParameter="{x:Reference btn}"/>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

AbsoluteLayoutPage.xaml.cs:

using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestBugs
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AbsoluteLayoutPage : ContentPage
    {
        public AbsoluteLayoutPage()
        {
            ButtonCommand = new Command(ButtonClick);
            InitializeComponent();
            BindingContext = this;
        }

        private void ButtonClick(object ob)
        {
            var button = ob as View;
            var bounds = btn.Bounds;
            var width = btn.Width;
            var height = btn.Height;
            Debug.WriteLine($"--- ButtonClick {bounds}, ({width},{height})---");

        }

        public Command ButtonCommand { get; set; }
    }
}

When click button, in VS Output pane:

[0:] --- ButtonClick {X=200 Y=200 Width=88 Height=48}, (88,48)---

Upvotes: 1

Jason
Jason

Reputation: 89102

this works for me

<Button Text="Click" Clicked="Clicked"  />

public void Clicked(object sender, EventArgs a)
    {
        var btn = (Button)sender;

        var w = btn.Width;
        var h = btn.Height;

        DisplayAlert("Dimesions", $"{w}x{h}","OK");

    }

Upvotes: 0

Related Questions