Xaml 数据触发器:如果数据可用,则隐藏占位符

Xaml 数据触发器:如果数据可用,则隐藏占位符,xaml,listview,data-binding,xamarin.forms,datatrigger,Xaml,Listview,Data Binding,Xamarin.forms,Datatrigger,目前我有两个图像:占位符图像,真实图像。如果真实图像的缩略图路径可用,我想隐藏占位符图像。因此,我认为我可以使用: 所需操作:占位符应消失,缩略图应显示 所需操作:占位符应保持隐藏,新缩略图应显示 所需操作:占位符应为visisble,缩略图应隐藏 这可以用数据触发器管理吗?怎么做 我试图通过代码(代码隐藏文件)设置可见性,但列表视图中的项目没有更新(显示占位符,尽管有缩略图可用,但只有在列表中滚动才能将缩略图带到前面) 此外,我使用占位符是因为当我有一个绑定,然后我在代码中更改图像的源时,绑定

目前我有两个图像:占位符图像,真实图像。如果真实图像的缩略图路径可用,我想隐藏占位符图像。因此,我认为我可以使用:

所需操作:占位符应消失,缩略图应显示

所需操作:占位符应保持隐藏,新缩略图应显示

所需操作:占位符应为visisble,缩略图应隐藏

这可以用数据触发器管理吗?怎么做

我试图通过代码(代码隐藏文件)设置可见性,但列表视图中的项目没有更新(显示占位符,尽管有缩略图可用,但只有在列表中滚动才能将缩略图带到前面)

此外,我使用占位符是因为当我有一个绑定,然后我在代码中更改图像的源时,绑定就消失了…

现在我使用:

<Image x:Name="placeholder" Aspect="AspectFit" HorizontalOptions="Center" WidthRequest="60" IsVisible="False">
    <Image.Triggers>
        <DataTrigger TargetType="Image"
                     Binding="{Binding ThumbnailFilePath}"
                     Value="{x:Null}">
            <Setter Property="IsVisible" Value="True" />
        </DataTrigger>
        <DataTrigger TargetType="Image"
                     Binding="{Binding ThumbnailFilePath, Path=Text.Length}"
                     Value="0">
            <Setter Property="IsVisible" Value="True" />
        </DataTrigger>
    </Image.Triggers>
</Image>
<Image x:Name="preview" Aspect="AspectFit" HorizontalOptions="Center" WidthRequest="60" Source="{Binding ThumbnailFilePath, Converter ={StaticResource ImageSourceConverter}}"/>
ThumbnailFilePath = null;
ThumbnailFilepath = "path/to/file.jpg";
ThumbnailFilePath = "old/path/to/file.jpg";
ThumbnailFilepath = "path/to/file.jpg";
ThumbnailFilePath = "path/to/file.jpg";
ThumbnailFilepath = null;
<Image x:Name="placeholder"
       BindingContext="{x:Reference Name=preview}"
       Aspect="AspectFit"
       HorizontalOptions="Center"
       WidthRequest="60"
       IsVisible="{Binding Path=Source, Converter ={StaticResource IsNullConverter}">
<Image x:Name="preview" 
       Aspect="AspectFit" 
       HorizontalOptions="Center"
       WidthRequest="60" 
       Source="{Binding ThumbnailFilePath, Converter ={StaticResource ImageSourceConverter}}"/>
class IsNullOrEmptyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string)
            return string.IsNullOrEmpty((string)value);

        return (value == null);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException("IsNullOrEmptyConverter can only be used one way.");
    }
}