DataTrigger for a Grid based on a Child

Jassim Al Rahma 1,526 Reputation points
2021-08-06T21:00:21.333+00:00

Hi,

In below code, How can I make a Grid's DataTrigger to set the Grid's HeightRequest to ZERO when the zeera_post_age is NULL or EMPTY?

<Grid Grid.Row="5" ColumnSpacing="0" RowSpacing="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Margin="5" Text="Age : " TextColor="Red" VerticalOptions="Center" />
    <Label Grid.Column="1" Margin="5" Text="{Binding post_age}" VerticalOptions="Center" />
</Grid>

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,390 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,131 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-08-09T02:12:48.027+00:00

    Hi JassimAlRahma-9056,

    Welcome to our Microsoft Q&A platform!

    About how to use DataTrigger in Xamarin, you can refer to the document: Xamarin.Forms Triggers.

    If you want to hide the Grid when post_age is NULL or EMPTY, set Grid's "IsVisible" property to "false" will be a better choice.

    The following is a simple demo.

    MainPage.xaml

    <ContentPage.BindingContext>  
        <loacl:MainPageViewModel/>  
    </ContentPage.BindingContext>  
      
    <StackLayout>  
        <Entry x:Name="entry"  
                Text="{Binding Post_age}"  
                Placeholder="required field" />  
      
        <Grid Grid.Row="5" ColumnSpacing="0" RowSpacing="0">  
            <Grid.ColumnDefinitions>  
                <ColumnDefinition Width="Auto" />  
                <ColumnDefinition Width="*" />  
            </Grid.ColumnDefinitions>  
      
            <Grid.Triggers>  
                <DataTrigger TargetType="Grid"  
                        Binding="{Binding Source={x:Reference postAgeLabel},  
                                        Path=Text.Length}"  
                        Value="0">  
                    <Setter Property="IsVisible" Value="False" />  
                </DataTrigger>  
            </Grid.Triggers>  
      
            <Label Grid.Column="0" Margin="5" Text="Age : " TextColor="Red" VerticalOptions="Center" />  
            <Label Grid.Column="1" Margin="5" x:Name="postAgeLabel" Text="{Binding Post_age}" VerticalOptions="Center" />  
        </Grid>  
    </StackLayout>  
    

    MainPageViewModel.cs

    class MainPageViewModel : INotifyPropertyChanged  
    {  
        public MainPageViewModel()  
        {  
            Post_age = "12";  
        }  
      
        string post_age;  
        public string Post_age  
        {  
            get => post_age;  
            set  
            {  
                if (value == null)  
                    return;  
                post_age = value;  
                OnPropertyChanged("Post_age");  
            }  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
      
        protected void OnPropertyChanged(string propertyName)  
        {  
            var handler = PropertyChanged;  
            if (handler != null)  
                handler(this, new PropertyChangedEventArgs(propertyName));  
        }  
    }  
    

    Regards,
    Kyle


    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