2016-08-29 3 views
0

Hallo allerseits Ich entwickle eine Xamarin App und ich habe ein Problem. Ich habe den nächsten Code in XAML:Wie kann ich dem Bindungswert der Bildquelle einen String hinzufügen?

<ListView x:Name="listName"> 
    <ListView.ItemTemplate> 
    <DataTemplate> 
     <ViewCell> 
     <StackLayout Orientation="Vertical"> 
      <Image Source="{Binding imageName}"></Image> 
      <Label Text="{Binding name}" TextColor="Black"></Label> 
     </StackLayout> 
     </ViewCell> 
    </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

Ich brauche die Bildquelle Eigenschaft ändern, um eine Zeichenfolge hinzuzufügen. Zum Beispiel:

<Image Source="{Binding imageName} + mystring.png"></Image> 
<Image Source="{Binding imageName + mystring.png}"></Image> 

Kann ich dies in XAML tun? eine Idee? ist möglich?

Antwort

1

Sie könnten einen Konverter verwenden, um dies zu tun.

public class ImagePostfixValueConverter 
    : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var path = value as string; 
     var postfix = parameter as string; 

     if (string.IsNullOrEmpty(postfix) || string.IsNullOrEmpty(path)) 
      return value; 

     return path + postfix; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

Dann auf Ihrer Seite:

<ContentPage.Resources> 
    <ResourceDictionary> 
     <local:ImagePostfixValueConverter x:Key="PostfixConverter"/> 
    </ResourceDictionary> 
</ContentPage.Resources> 

Und dann in Ihrer Bindung:

<Image Source="{Binding imageName, Converter={StaticResouce PostfixConverter}, ConverterParameter=mystring.png}"></Image> 
Verwandte Themen