如何更改点击列表视图项图像xamarin?

时间:2019-02-15 11:56:37

标签: c# xamarin xamarin.forms

我有我正在使用转换器更改图像的图像列表。当点击图像时,所有图像均被更改,我仅需要在MVVM中更改点击的图像。有人请指导。

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? "progressicon.png" : "cloud_download.png";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

Download icon icon change

<StackLayout>
    <Label Text="{Binding DateTime,Converter={StaticResource timestampconverter }}" HeightRequest="150" WidthRequest="300" />
    <Grid>
        <ImageButton BackgroundColor="Transparent"
                                HeightRequest="40"  
                                IsVisible="{Binding ToDownload}" 
                                Source="{Binding BindingContext.DownloadIcon, Converter={StaticResource downloadiconconverter},Source={x:Reference listview}}"
                                WidthRequest="35" Command ="{Binding BindingContext.Download, Source={x:Reference listview}}" CommandParameter="{Binding .}" />
        <ImageButton BackgroundColor="Transparent"
                                HeightRequest="40" 
                                IsVisible="{Binding ToUpload}"    
                                Source="{Binding BindingContext.UploadIcon, Converter={StaticResource uploadiconconverter},Source={x:Reference listview}}"
                                WidthRequest="35"  Command ="{Binding BindingContext.Upload, Source={x:Reference listview}}" CommandParameter="{Binding .}" />
    </Grid>
</StackLayout>

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

下载图标转换器:

public class Downloadiconconverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)value ? "progressicon.png":"cloud_download.png";
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return false; // not needed
    }
}

XAML:

<StackLayout>
    <Label Text="{Binding DateTime,Converter={StaticResource timestampconverter }}" HeightRequest="150" WidthRequest="300" />
    <Grid>
        <ImageButton BackgroundColor="Transparent"
                                HeightRequest="40"  
                                IsVisible="{Binding ToDownload}" 
                                Source="{Binding IsDownloading, Converter={StaticResource downloadiconconverter},Source={x:Reference listview}}"
                                WidthRequest="35" Command ="{Binding BindingContext.Download, Source={x:Reference listview}}" CommandParameter="{Binding .}" />
        <ImageButton BackgroundColor="Transparent"
                                HeightRequest="40" 
                                IsVisible="{Binding ToUpload}"    
                                Source="{Binding IsUploading, Converter={StaticResource uploadiconconverter},Source={x:Reference listview}}"
                                WidthRequest="35"  Command ="{Binding BindingContext.Upload, Source={x:Reference listview}}" CommandParameter="{Binding .}" />
    </Grid>
</StackLayout>

模型类别:

var list = new List<object>();
list.Add(new { DateTime = "15/02/2019", ToDownload = true, IsDownloading=false });

查看型号代码:

private bool? isDownloading;

public bool? IsDownloading
 {
    get { return isDownloading; }
    set
    {
       isDownloading = value;
       OnPropertyChanged("IsDownloading");
    }
 }

类似地,您可以上传场景案例。