indubitablee
indubitablee

Reputation: 8206

Xamarin XAML Conditional Styling

I have a StackLayout in a ListView that I want to hide (IsVisible="false") based on whether the object has a PerformedDate value or not (defaulted to null).

Right now, I'm trying to use DataTriggers on the StackLayout using a label's text as an indicator for whether or not the object has a PerformedDate:

<Label x:Name="PerformedDateLabel" Text="{Binding PerformedDate}" IsVisible="True"/>
<StackLayout Padding="0, 25, 0, 0" Spacing="0">
    <Label Text="{Binding Date, StringFormat='{0:dd}'}" FontSize="Small" HorizontalOptions="Center"/>
    <Label Text="{Binding Date, StringFormat='{0:MMM}'}" HorizontalOptions="Center"/>
    <StackLayout.Triggers>
        <DataTrigger TargetType="StackLayout" Binding="{Binding Source={x:Reference PerformedDateLabel}, Path=Text}" Value="">
            <Setter Property="IsVisible" Value="False"/>
        </DataTrigger>
    </StackLayout.Triggers>
</StackLayout>

It doesn't hidee the StackLayout when a PerformedDate exists. Any ideas?

I've also tried the Path=Text.Length Value="0" approach.

Upvotes: 2

Views: 4618

Answers (1)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

You could use an additonal Property HasPerformedDate

C#

public DateTime? PerformedDate
{
    get { return _performedDate; }
    set
    {
        _performedDate = value; 
        OnPropertyChanged();
        OnPropertyChanged(nameof(HasPerformedDate));
    }
}

public bool HasPerformedDate => PerformedDate.HasValue;

Xaml

<Label x:Name="PerformedDateLabel" Text="{Binding PerformedDate}" IsVisible="True"/>
<StackLayout Padding="0, 25, 0, 0" Spacing="0" IsVisible="{Binding HasPerformedDate}">
  <Label Text="{Binding PerformedDate, StringFormat='{0:dd}'}" FontSize="Small" HorizontalOptions="Center"/>
  <Label Text="{Binding PerformedDate, StringFormat='{0:MMM}'}" HorizontalOptions="Center"/>
</StackLayout>

Upvotes: 3

Related Questions