Databinding into grid's columnDefinition width RRS feed

  • Question

  • User386367 posted

    Hi, I am having problems with binding into grid's columndefinition width proprety. I tried TestWidth.Value as well as just double and string. Any idea whats the problem?

    `<ColumnDefinition Width="{Binding Parent.SizeModifier.TestWidth, Source={x:Reference Self}}"></ColumnDefinition>`
        class SizeModifier
            public int DefaultWindowWidth { get; set; }
            public int DefaultWindowHeight { get; set; }
            public int DefaultWindowDPI { get; set; }
            public double WidthModifier { get; set; }
            public double HeightModifier { get; set; }
            public double DPIModifier { get; set; }
            public int DeviceWidth { get; set; }
            public int DeviceHeight { get; set; }
            public int DeviceDPI { get; set; }
            public GridLength TestWidth { get; set; }
            public string Test = "400";
            public SizeModifier()
                DefaultWindowWidth = 1280;
                DefaultWindowHeight = 800;
                DefaultWindowDPI = 160;
                DeviceDPI = DependencyService.Get<IGetScreenSizes>().GetDisplayDpi();
                DeviceWidth = DependencyService.Get<IGetScreenSizes>().GetDisplayWidth();
                DeviceHeight = DependencyService.Get<IGetScreenSizes>().GetDisplayHeight();
                DPIModifier = (double)DeviceDPI / (double)DefaultWindowDPI;
                WidthModifier = (double)DeviceWidth / (double)DefaultWindowWidth*DPIModifier;
                HeightModifier = (double)DeviceHeight / (double)DefaultWindowHeight*DPIModifier;
                TestWidth = new GridLength((double)400 * WidthModifier, GridUnitType.Absolute);
    Tuesday, July 9, 2019 2:45 PM

All replies

  • User74518 posted

    Binding requires a declared static BindableProperty (that has the same name as the backing property plus "Property" suffix)

    ```` public static BindableProperty TestWidthProperty = BindableProperty.Create(nameof(TestWidth), typeof(GridLength), typeof(SizeModifier), defaultValue: GridLength.Star);

    public GridLength TestWidth { get =>(GridLength)GetValue(TestWidthProperty); set => SetValue(TestWidthProperty, value); } ````

    Tuesday, July 9, 2019 4:14 PM
  • User386367 posted

    @Amar_Bait This is not a problem. I have used same structure to bind the values. Everything else works, but SizeModifier.

     ApplicationBindings ApplicationBind { get; set; }
        class ApplicationBindings
            public Navigation AppNavigation { get; set; }
            public Battery BatteryStatus { get; set; }
            public UserData UserData { get; set; }
            public DeviceData DeviceData { get; set; }
            public VitalsData VitalsData { get; set; }
            public SizeModifier SizeModifier { get; set; }
    Wednesday, July 10, 2019 7:18 AM
  • User386367 posted

    In boxview's widthrequest following works. {TemplateBinding Parent.BindingContext.SizeModifier.TestWidth.Value}

    I am not able to use it in columndefinition's width because of https://github.com/xamarin/Xamarin.Forms/issues/4835

    Wednesday, July 10, 2019 7:44 AM
  • User386367 posted

    Following works. {Binding BindingContext.SizeModifier.TestWidth, Source={x:Reference Self}}

    seems like no need for Parent keyword.

    Wednesday, July 10, 2019 8:14 AM
  • User394161 posted


    I have more or less the same issue. I am working on some template control currently. It uses Grid to place two labels.

    the code behind is the following

        public static readonly BindableProperty PromptWidthProperty = BindableProperty.Create(
                                                defaultValue: GridLength.Star);
        public GridLength PromptWidth
                return (GridLength)GetValue(PromptWidthProperty);
                SetValue(PromptWidthProperty, value);

    XAML code

            <Grid Margin="0,0,0,12" 
                    BackgroundColor="{TemplateBinding BackgroundColor}">
                    <ColumnDefinition Width="{TemplateBinding PromptWidth}"/>
                    <ColumnDefinition Width="*"/>
                    <RowDefinition Height="Auto"/>


    The following exception is thrown as soon as I enable TemplateBinding for column width. The control works fine without it. What can be the issue ?

    Exception has been thrown by the target of an invocation.

    Thank you in advance

    Friday, April 24, 2020 8:49 PM
  • User386367 posted

    @Alex0407 Have you tried my solution? Set parent container's xname to x:Name="Self

    and then just use normal binding with source reference to that element

    {Binding BindingContext.SizeModifier.TestWidth, Source={x:Reference Self}}

    Monday, April 27, 2020 12:30 PM
  • User394161 posted

    Hi Trixo

    thank you for your fast feedback. I have updated the solution accordingly to your recommendations and it works.

    it was before ColumnDefinition Width="{TemplateBinding PromptWidth}"

    it is now ColumnDefinition Width="{Binding PromptWidth, Source={x:Reference this}}"

    where "this" is declared as below x:Name="this"

    If TemplateBinding is used then the following exception is thrown

    System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'

    Why does TemplateBinding work fine for other bindable properties of the same control ?

    For example, the control has a property declared like this

    and it works fine always.

    What is the possible reason of such difference ? Data type ?

    Monday, April 27, 2020 5:33 PM