question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked JassimAlRahma-9056 commented

DataTrigger for a Grid based on a Child

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

dotnet-csharpdotnet-xamarindotnet-runtime
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

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered JassimAlRahma-9056 commented

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.

· 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.

How can I do the trigger when the value coming from MySQL is null?

I tried below but did not work:

 <Grid.Triggers>
 <DataTrigger TargetType="Grid" Binding="{Binding Source={x:Reference LabelPostAllergies}, Path=Text.Length}" Value="{x:Null}">
     <Setter Property="IsVisible" Value="False" />
 </DataTrigger>
 </Grid.Triggers>


0 Votes 0 ·