微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改ListView中文本的颜色

如何解决更改ListView中文本的颜色

ListView认颜色是红色,我想更改它。我尝试在Label内使用ListView,但是它什么也不返回(空的scree,没有值)。

您看到Itemsource中的ListViewLabel中的Text的值是Bind,这就是空白屏幕的原因。

如何更改文本的颜色?

<ListView
    BackgroundColor="#004B86"
    ItemsSource="{Binding Attributes}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Label 
            Text="{Binding Attributes}"
            TextColor="White">
            </Label>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

属性

foreach (var item in _credential.CredentialAttributesValues)
{
    _attributes.Add(item.Name.ToString());
    _attributes.Add(item.Value.ToString());
}    

可绑定属性

private ObservableCollection<string> _attributes = new ObservableCollection<string>();
public ObservableCollection<string> Attributes
{
    get
    {
        return _attributes;
    }
    set
    {
        this.RaiseAndSetIfChanged(ref _attributes,value);
    }
}

解决方法

DetailedCell中,必须有一些标签可以用来设置TitleSubtitle。因此,您可以在此处更改这些标签的TextColor属性,以更改文本颜色。

更新:

<ListView BackgroundColor="#004B86" ItemsSource="{Binding Attributes}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            
            <ViewCell>
                
                <Label 
                    Text="{Binding Attributes}" TextColor="White">
                </Label>
                
            </ViewCell>

        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
,

所以我不确定我是否完全理解需要做什么,但是如果您想更改某些东西的颜色,可以尝试执行以下操作。我确实更新了一些变量名以使其更合适。让我知道这是否正确

您加载ListView的页面将如下所示,请注意x:Name属性:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:XamarinFormsPlayground.Controls;assembly=XamarinFormsPlayground" xmlns:local="clr-namespace:XamarinFormsPlayground.Resources"
             x:Class="XamarinFormsPlayground.MainPage"
             x:Name="root">
    <ListView
    BackgroundColor="{Binding ListViewColor,Mode=TwoWay}"
    ItemsSource="{Binding Colors,Mode=TwoWay}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label 
                        Text="{Binding .}"
                        TextColor="White">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding BindingContext.ChangeColorCommand,Source={x:Reference root}}" CommandParameter="{Binding .}"/>
                        </Label.GestureRecognizers>
                    </Label>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

在后面的代码中,我将BindingContext设置为我绑定到的ViewModel的绑定视图,该视图模型如下所示:

public class BasicViewModel : BindableObject
    {
        private ObservableCollection<string> colors = new ObservableCollection<string>();
        private Color listViewColor = Color.FromHex("#004B86");

    public Color ListViewColor {
        get {  return listViewColor; }
        set {
            listViewColor = value;
            OnPropertyChanged("ListViewColor");
        }
    }
    public ICommand ChangeColorCommand => new Command<string>(x => ChangeColor(x));
    public ObservableCollection<string> Colors {
        get {
            return colors;
        }
        set {
            colors = value;
            OnPropertyChanged("Colors");
        }
    }

    public BasicViewModel()
    {
        LoadData();
    }

    public void LoadData()
    {
        colors.Add("Red");
        colors.Add("Blue");
        colors.Add("Green");
        colors.Add("White");
        colors.Add("Orange");

        Colors = colors;
    }

    private void ChangeColor(string color)
    {
        if(!string.IsNullOrEmpty(color))
        {
            var selectedColor = typeof(Color).GetRuntimeFields()
                .Where(x => x.IsPublic)
                .Where(x => x.IsStatic)
                .Where(x => x.FieldType == typeof(Color))
                .Where(x => x.Name.ToLower() == color.ToLower())
                .First();
        
            ListViewColor = (Color) selectedColor.GetValue(null);

        }
    }
}

说明:

在XAML中,我们将颜色名称绑定为{Binding .},因为DataTemplate内的项目使用ItemSource作为其BindingContext,并且由于它只是字符串的集合,它只有自己要绑定,这就是.绑定的意思。

然后,我们将Label上的TapGestureRecognizer绑定到整个页面的BindingContext,从而获得命令绑定的有趣语法。

代码的神奇之处在于ChangeColor方法,它将遍历Color结构的runtimefields并找到与我们点击的名称匹配的颜色。该代码的.ToLower()部分仅用于消除颜色名称中的大小写敏感性。

使用上述方法,您应该也可以对其进行扩展以满足您的需求。如果要更改ListView实际呈现的Text的颜色,则只需修改上述XAML的绑定。实际上,您要做的就是为整个列表视图定义常规颜色(例如黑色),然后在Color="White"上放置Label的位置,只需将其绑定设置为{Binding BindingContext.ChangeColorCommand,Source={x:Reference root},Mode=TwoWay}"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。