Grid row height TemplateBinding throwing exception

Raihan Iqbal 121 Reputation points
2020-12-23T06:10:54.267+00:00

I'm trying to hide a row of a Grid in my ControlTemplate based on the value of a boolean BindableProperty.

<ContentPage.Resources>

...

    <ControlTemplate x:Key="HeaderWithNavigation">
                <Grid x:Name="HeaderWithNavigationGrid">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.1*" />
                        <RowDefinition Height="0.8*" />
                        <RowDefinition Height="{TemplateBinding ShowFooter, Converter={StaticResource GridRowVisibilityConverter}}" />
                    </Grid.RowDefinitions>
                </Grid>
     </ControlTemplate>

...

</ContentPage.Resources>

The ShowFooter is defined as such:

public static readonly BindableProperty ShowFooterProperty = BindableProperty.Create(nameof(ShowFooter), typeof(bool), typeof(MyDefaultTemplatePage), default(bool));
        public bool ShowFooter
        {
            get => (bool)GetValue(ShowFooterProperty);
            set => SetValue(ShowFooterProperty, value);
        }

I borrowed the coverter from here. I dont get any compilation errors but when I load the page that uses this template, the app crashes with a System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2020-12-23T09:17:02.28+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I tested your code in a basic demo and faced the same issue. It's caused by the limit of TemplateBinding, check this link. To fix this issue, try using Binding instead.

       <ControlTemplate x:Key="_template">  
           <Grid>  
               <Grid.RowDefinitions>  
                   <RowDefinition Height="25" />  
                   <RowDefinition Height="35" />  
                   <RowDefinition Height="{TemplateBinding IsShowing, Converter={StaticResource _converter}}" />  
               </Grid.RowDefinitions>  
               ...  
           </Grid>  
       </ControlTemplate>  
    

    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.


0 additional answers

Sort by: Most helpful