如何使用Xamarin表单中的绑定数据更改viewcell的外观?
我想做的是,我创建了一个ViewCell并将其绑定到ListView。在ViewCell中,我有一个标题标签,我想根据来自数据库的数据更改它。 对此,最佳做法是什么 这是我的代码块 模型-如何使用Xamarin表单中的绑定数据更改viewcell的外观?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我想做的是,我创建了一个ViewCell并将其绑定到ListView。在ViewCell中,我有一个标题标签,我想根据来自数据库的数据更改它。 对此,最佳做法是什么 这是我的代码块 模型- public class helplineservices { public string title { get; set; } public bool isenable { get; set; } } 可视单元- public class HelpLine
public class helplineservices
{
public string title { get; set; }
public bool isenable { get; set; }
}
可视单元-
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
}
列表视图-
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来更改标题颜色。
请帮助我。您必须像对文本属性所做的那样绑定TextColor,然后使用IValueConverter将布尔值转换为颜色 比如:
title.SetBinding(Label.TextColorProperty, new Binding("isenable", BindingMode.Default, new BooleanToColorConverter()));
您的IValueConverter应该是
公共类布尔色转换器:IValueConverter
{
PS:未测试
我尝试过,但我没有从ViewCell类中的数据库中获取可在IValueConverter中使用的布尔值。哪是您的模型?添加了模型。我想检查isenable属性并更改标题的文本颜色。我不知道如何在条件检查中使用isenable属性。我已修改了答案
#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
}