Xamarin XAML条件样式



我在ListView中有一个StackLayout,我想根据对象是否具有PerformedDate值(默认为null)来隐藏它(IsVisible="false")。

现在,我正在尝试在StackLayout上使用DataTriggers,使用label的文本作为对象是否具有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>

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

我还尝试了Path=Text.LengthValue="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>

相关内容

  • 没有找到相关文章

最新更新