Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Xamarin表单中的绑定数据更改viewcell的外观?_Xamarin_Xamarin.forms - Fatal编程技术网

如何使用Xamarin表单中的绑定数据更改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

我想做的是,我创建了一个ViewCell并将其绑定到ListView。在ViewCell中,我有一个标题标签,我想根据来自数据库的数据更改它。 对此,最佳做法是什么

这是我的代码块

模型-

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
}