2017-03-21 31 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

相关问题