2017-03-24 6 views
0
ListView listView = new ListView(); 
listView.ItemTemplate = new DataTemplate(typeof(CustomVeggieCell)); 
listView.ItemsSource = sample; 
Content = new StackLayout 
{ 
    Children ={ 
     listView, 
    } 
}; 


public class CustomVeggieCell : ViewCell 
{ 
public CustomVeggieCell() 
{ 
     var image = new Image(); 
     var typeLabel = new Label { }; 


     typeLabel.SetBinding(Label.TextProperty, new Binding("contact")); 

     var string = typeLabel.Text; 

     if (typeLabel.Text == "Send") 
     { 
      image.Source = "Send.png"; 
     } 
     else 
     { 
      image.Source = "draft.png"; 
     } 


     var horizontalLayout = new StackLayout(); 
     horizontalLayout.Children.Add(image); 


     View = horizontalLayout; 

    } 
} 

私は、XamarinフォームでJson Webサービスの応答を使用してリストビューを作成しました。私は値に応じて画像を表示する必要があります。 バインド値を文字列に格納できませんでした。常にnullを返します。バインディングラベルのテキストを保存します。これを達成するためにホ?あなたはIValueConverterバインディングコンテキストのラベル値をxamarinフォームの文字列に保存する方法

public class CustomVeggieCell : ViewCell 
{ 
    public CustomVeggieCell() 
    { 
     var image = new Image(); 

     image.SetBinding(Image.SourceProperty, new Binding("contact", BindingMode.Default, new ConvertTextToSource())); 

     var horizontalLayout = new StackLayout(); 
     horizontalLayout.Children.Add(image); 


     View = horizontalLayout; 

    } 

} 

のようなものを使用することができます

答えて

0

その後コンバータ

 public class ConvertTextToSource : IValueConverter 
     { 

      #region IValueConverter implementation 

      public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
      { 
       if (value != null && value is string) { 

        string text = ((string)value); 

        if (text == "Send") 
        { 
         return "Send.png"; 
        } 
        else 
        { 
         return "draft.png"; 
        } 
       } 
       return ""; 
      } 

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

      #endregion 
     } 

それは...それは私のために働い

+0

おかげで動作します必要があります:) – Jeni

関連する問題