我有ViewView的ListView表示来自WebService的数据。我在来自WebService的数据中有布尔标志,根据该标志我需要更改ViewCell文本的颜色。如果flag为True,则设置颜色为红色,否则为白色。怎么做?
我的ListView和ViewCell如下:
ListView listof_datewiseRecord = new ListView {
BackgroundColor=ColorResources.PageBackgroundColor,
ItemTemplate = new DataTemplate (typeof(dataCell)),
ItemsSource = listofdate,
RowHeight=70,
};
public class dataCell:ViewCell
{
public dataCell ()
{
var date = new Label () {
FontFamily = "HelveticaNeue-Medium",
FontSize = 13,
TextColor = ColorResources.TextColor,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start
};
date.SetBinding (Label.TextProperty, "date");
var day = new Label (){
FontFamily = "HelveticaNeue-Medium",
FontSize = 13,
TextColor = ColorResources.TextColor,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start
};
day.SetBinding (Label.TextProperty,"weekday");
var date_and_day = new StackLayout {
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start,
WidthRequest=90,
Children={
date,
day
}
};
var inTime = new Label () {
FontFamily = "HelveticaNeue-Medium",
FontSize = 13,
WidthRequest=60,
TextColor = ColorResources.TextColor,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start
};
inTime.SetBinding (Label.TextProperty,"In");
var outTime = new Label () {
FontFamily = "HelveticaNeue-Medium",
WidthRequest=60,
FontSize = 13,
TextColor = ColorResources.TextColor,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start
};
outTime.SetBinding (Label.TextProperty,"Out");
var totalTime = new Label () {
FontFamily = "HelveticaNeue-Medium",
WidthRequest=60,
FontSize = 13,
TextColor = ColorResources.TextColor,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start
};
totalTime.SetBinding (Label.TextProperty, "totalInTime");
var btnDetails = new Image () {
WidthRequest=40,
HeightRequest=40,
BackgroundColor=Color.Black,
Source="info.png",
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
var cellLayout = new StackLayout {
Spacing = 5,
Padding = new Thickness (5, 5, 5, 5),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = {
date_and_day,
inTime,
outTime,
totalTime,
btnDetails
}
};
this.View = cellLayout;
}
}
在ListView中加载数据的模型如下:
public class DateWiseData
{
public string _Date { get; set; }
public List<DateData> dateData { get; set; }
}
public class DateData
{
public string uid { get; set; }
public string employeeName { get; set; }
public string _date { get; set; }
public string fin { get; set; }
public string fout { get; set; }
public string weekday { get; set; }
public string totalInTime { get; set; }
public Boolean Is_Loc_Device_Changed { get; set; }
}
如果Is_Loc_Device_Changed设置为True,那么我需要将ViewCell的所有标签更改为红色,否则为白色或保持不变。 提前谢谢!
答案 0 :(得分:0)
您想要的是使用converter
public class InvertBoolenConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
}
在你的绑定代码中:
runtime.SetBinding(Label.TextProperty,new Binding("Runtime", BindingMode.Default,
new InvertBoolenConverter(), null));
或者在xaml。