Xamarin XAML条件样式

时间:2016-05-06 18:36:13

标签: xaml xamarin styles conditional datatrigger

根据对象是否具有StackLayout值,我想要隐藏ListView IsVisible="false" PerformedDate(默认为{null 1}})。

现在,我正在尝试使用DataTriggers上的StackLayout使用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>

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用其他属性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>