2017-03-21 29 views
0

我想要做的是,我創建了一個ViewCell並將其綁定到ListView。在ViewCell中,我有標題標籤,我想根據數據庫中的數據進行更改。 這將是什麼最佳做法?如何使用Xamarin Forms中的綁定數據更改視單元的外觀?

這裏我的代碼塊

型號 -

public class helplineservices 
    { 
     public string title { get; set; } 
     public bool isenable { get; set; } 
    } 

ViewCell -

public class HelpLineCell : ViewCell 
    { 
     #region binding view cell logic 
     public HelpLineCell() 
     { 
      BlackLabel title = new BlackLabel 
      { 
       FontFamily = Device.OnPlatform(
          "Roboto-Black", 
          null, 
          null), 
       FontSize = Device.OnPlatform(
          (ScreenSize.getscreenHeight()/47), 
          (ScreenSize.getscreenHeight()/47), 
          14 
         ), 
       HorizontalTextAlignment = TextAlignment.Center, 
       TextColor = Color.FromHex("#FFFFFF"), 
       WidthRequest = ScreenSize.getscreenWidth() 
      }; 
      title.SetBinding(Label.TextProperty, "title"); 

      this.View = title; 
     } 
     #endregion 
} 

的ListView -

var HelpList = new ListView 
      { 
       IsPullToRefreshEnabled = true, 
       HasUnevenRows = true, 
       BackgroundColor = Color.Transparent, 
       RefreshCommand = RefreshCommand, 
       //row_list is a list that comes from database 
       ItemsSource = row_list, 
       ItemTemplate = new DataTemplate(typeof(HelpLineCell)), 
       SeparatorVisibility = SeparatorVisibility.None 
      }; 

我想通過檢查一個布爾值更改標題顏色isenable的價值它來自數據庫。 請幫幫我。

回答

0

你必須綁定TEXTCOLOR就像你做你的Text屬性,然後用的IValueConverter布爾值轉換爲彩色

喜歡的東西:

title.SetBinding(Label.TextColorProperty, new Binding("isenable", BindingMode.Default, new BooleanToColorConverter())); 

你的IValueConverter應該是這樣的

公共類BooleanToColorConverter:的IValueConverter {

#region IValueConverter implementation 

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

      if(((bool) value) == true) 
       return Color.Red; 
      else 
       return Color.Black; 
     } 
     return Color.Black; 
    } 

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

    #endregion 
} 

PS:未測試...

A useful article

+0

我嘗試過,但我沒有從ViewCell類數據庫,能夠在的IValueConverter使用越來越布爾值。 – Atul

+0

哪個是您的模特? –

+0

增加了模型。我想檢查isenable屬性並更改標題的文本顏色。我沒有得到如何我可以使用isenable財產在條件檢查 – Atul

相關問題