【问题标题】:Data Triggers: Hide placeholder if data is available数据触发器:如果数据可用则隐藏占位符
【发布时间】:2017-08-15 20:02:10
【问题描述】:

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

<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 = null;
ThumbnailFilepath = "path/to/file.jpg";

所需的操作:占位符应该消失,缩略图应该显示出来。

ThumbnailFilePath = "old/path/to/file.jpg";
ThumbnailFilepath = "path/to/file.jpg";

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

ThumbnailFilePath = "path/to/file.jpg";
ThumbnailFilepath = null;

期望的操作:占位符应该是可见的,缩略图应该是隐藏的。

这可以通过数据触发器进行管理吗?如何?

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

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

【问题讨论】:

    标签: xaml listview data-binding xamarin.forms datatrigger


    【解决方案1】:

    现在我使用View-To-View Bindings:

    <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}}"/>
    

    上述方法似乎有效。我还必须删除代码中IsVisible 的所有手动设置。

    这是我使用的IValueConverter

    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.");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      相关资源
      最近更新 更多