Xamarin XAML 条件样式

Xamarin XAML Conditional Styling

我在 ListView 中有一个 StackLayout,我想根据对象是否具有 PerformedDate 值(默认为null).

现在,我正在尝试在 StackLayout 上使用 DataTriggers,使用 label 的文本作为对象是否具有 [=15] 的指示符=]:

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

PerformedDate 存在时,它不会隐藏 StackLayout。 有什么想法吗?

我也试过 Path=Text.Length Value="0" 方法。

您可以使用额外的 属性 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>