Monday, September 10, 2018

How can I make it so a HeightRequest inside a template is ignored if not specified in the XAML that uses the template?

Leave a Comment

This template takes as a binding parameter HeaderHeight. Is there a way that I can make HeaderHeight ignored by the StackLayout if it's not specified in the XAML that calls the template?

<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Japanese;assembly=Japanese" x:Class="Japanese.Templates.HeaderTemplate" x:Name="this">     <ContentView.Triggers>         <Trigger TargetType="local:Templates.HeaderTemplate" Property="HeaderType" Value="Custom">             <Setter Property="Content">                 <Setter.Value>                     <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="0" Margin="0">                         <StackLayout HeightRequest="{Binding HeaderHeight, Source={x:Reference this}}" Orientation="Vertical" Spacing="0" Margin="0" >                             <Label Text="ABC" HorizontalOptions="Start" VerticalOptions="EndAndExpand" />                         </StackLayout>                     </StackLayout>                 </Setter.Value>             </Setter>         </Trigger>     </ContentView.Triggers> </ContentView> 

1 Answers

Answers 1

You can set the default value of HeaderHeight to -1

The UI should go back to normal

HeaderHeight = -1; 

Once it's a BindableProperty, it should look like this:

public static readonly BindableProperty HeaderHeightProperty = BindableProperty.Create(nameof(HeaderHeight), typeof(double), typeof(HeaderTemplate), -1); public double HeaderHeight {     get => (double)GetValue(HeaderHeightProperty);     set => SetValue(HeaderHeightProperty, value); } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment