question

jokertn avatar image
0 Votes"
jokertn asked jokertn commented

How to bind RowDefinition

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.






dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered jokertn commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks a lot , u solved my problem

0 Votes 0 ·