根据Xamarin中的布尔值绑定Label的TextColor

本文关键字:Label TextColor 绑定 布尔值 根据 Xamarin | 更新日期: 2023-09-27 18:15:31

我有ListView与ViewCell代表数据来自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的所有标签更改为红色,否则为白色或保持原样。提前感谢!

根据Xamarin中的布尔值绑定Label的TextColor

您需要的是使用转换器

简单的例子:

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));