How to bind RowDefinition

jokertn 61 Reputation points
2021-06-05T16:53:12.733+00:00

I have this binding in my XAML

        <Grid RowDefinitions="{Binding Deff}">
            <BoxView Grid.RowSpan="2" BackgroundColor="White">

            </BoxView>
            <Grid>
                <Path Data="M0,0H375V129.651s-9.025,2.382-55.426-5.3-70.141-41.09-130.3-41.176-83.544,33.5-130.893,41.176S0,129.651,0,129.651Z"
                      Aspect="Fill" Fill="#00a758"/>
            </Grid>

        </Grid>

and in my class c# :

    public partial class Test:  ContentPage
    {
        public GridLength Deff { get; set; }

        public Test()
        {
            InitializeComponent();
            if (Device.Idiom == TargetIdiom.Tablet)
            {
                Deff = 80;
            }
            else
            {

                Deff = 20;

            }
BindingContext = this;
        }
it dosent work , even i tried  this code also 
    public partial class Test:  ContentPage
    {
        public RowDefinitionCollection Deff { get; set; }

        public Test()
        {
            InitializeComponent();
            if (Device.Idiom == TargetIdiom.Tablet)
            {
                Deff = (RowDefinitionCollection)new RowDefinitionCollectionTypeConverter().ConvertFromInvariantString("80,*");
            }
            else
            {
                Deff = (RowDefinitionCollection)new RowDefinitionCollectionTypeConverter().ConvertFromInvariantString("20,*");

            }


            BindingContext = this;
        }

and same problem dosent work.

Any solution please.

Developer technologies | .NET | Xamarin
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-06-07T02:10:56.287+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To set data binding to the RowDefinition property, please create a parameter of RowDefinitionCollection type. Then add instances of RowDefinition with the desired Height to the collection.

    Check the code:

       //page.xaml  
       <Grid RowDefinitions="{Binding MyRows}">  
           <Label Text="row_1" Grid.Row="0"/>  
           <Label Text="row_2" Grid.Row="1"/>  
           <Label Text="row_3" Grid.Row="2"/>  
           <Label Text="row_4" Grid.Row="3"/>  
       </Grid>  
         
       //page.xaml.cs  
       public partial class TestPage : ContentPage  
       {  
           public RowDefinitionCollection MyRows { get; set; }  
         
           public TestPage()  
           {  
               InitializeComponent();  
         
               MyRows = new RowDefinitionCollection() {  
                   new RowDefinition { Height = GridLength.Auto },  
                   new RowDefinition { Height = 50 },  
                   new RowDefinition { Height = GridLength.Star },  
                   new RowDefinition { Height = 100 }  
               };  
         
               BindingContext = this;  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.