C# 更改ListView中文本的颜色
C# 更改ListView中文本的颜色,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,ListView的默认颜色是红色,我想更改它。我试图在列表视图中使用标签,但它什么也不返回(屏幕为空,没有值) 正如您在ListView中看到的Itemsource和Text中的Label是Bind到相同的值,这就是出现空屏幕的原因 如何更改文本的颜色 属性 foreach(凭证.CredentialAttributesValues中的变量项) { _attributes.Add(item.Name.ToString()); _attributes.Add(item.Value.ToStri
ListView
的默认颜色是红色,我想更改它。我试图在列表视图
中使用标签
,但它什么也不返回(屏幕为空,没有值)
正如您在ListView
中看到的Itemsource
和Text
中的Label
是Bind
到相同的值,这就是出现空屏幕的原因
如何更改文本的颜色
属性
foreach(凭证.CredentialAttributesValues中的变量项)
{
_attributes.Add(item.Name.ToString());
_attributes.Add(item.Value.ToString());
}
可绑定属性
private observetecollection\u attributes=new observetecollection();
公共可观测集合属性
{
得到
{
返回_属性;
}
设置
{
this.RaiseAndSetIfChanged(ref\u属性,值);
}
}
在您的DetailedCell
中,必须有一些标签,您可以在其中设置标题
和副标题
。因此,您可以更改这些标签的TextColor
属性来更改文本颜色
更新:
在您的
DetailedCell
中,必须有一些标签,您可以在其中设置标题
和副标题
。因此,您可以更改这些标签的TextColor
属性来更改文本颜色
更新:
所以我不确定自己是否完全理解了需要发生的事情,但是如果您想更改某些东西的颜色,可以尝试执行以下操作。我确实更新了一些变量名,使之更合适。让我知道这是否正确 加载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);
}
}
}
公共类基本视图模型:BindableObject
{
私有ObservableCollection颜色=新ObservableCollection();
私有颜色列表VIEWCOLOR=Color.FromHex(“#004B86”);
公共颜色列表视图颜色{
获取{return listViewColor;}
设置{
listViewColor=值;
OnPropertyChanged(“ListViewColor”);
}
}
公共ICommand ChangeColorCommand=>new命令(x=>ChangeColor(x));
公共可观察收集颜色{
得到{
返回颜色;
}
设置{
颜色=价值;
不动产变更(“颜色”);
}
}
公共基本视图模型()
{
LoadData();
}
公共void LoadData()
{
颜色。添加(“红色”);
颜色。添加(“蓝色”);
颜色。添加(“绿色”);
颜色。添加(“白色”);
颜色。添加(“橙色”);
颜色=颜色;
}
私有void ChangeColor(字符串颜色)
{
如果(!string.IsNullOrEmpty(颜色))
{
var selectedColor=typeof(Color).GetRuntimeFields()
.其中(x=>x.IsPublic)
.其中(x=>x.IsStatic)
.其中(x=>x.FieldType==typeof(颜色))
.Where(x=>x.Name.ToLower()==color.ToLower())
.First();
ListViewColor=(颜色)selectedColor.GetValue(空);
}
}
}
说明:
在XAML中,我们将颜色名称绑定为{Binding.}
,因为DataTemplate
中的项目使用ItemSource
作为其绑定上下文,并且由于它只是字符串的集合,所以它只需要绑定到自己,这就是
绑定的含义
然后,我们将标签上的TapGestureRecognitor绑定到整个页面的BindingContext,从而获得命令绑定的有趣语法
代码的神奇部分在于ChangeColor方法,它将遍历Color结构的runtimefields并找到与我们点击的名称匹配的颜色。代码的.ToLower()部分只是用来否定颜色名称中的任何大小写敏感性
使用上述方法,您应该能够扩展它以满足您的需求。如果您想要更改ListView实际呈现的文本的颜色,那么只需要稍微修改上面的XAML绑定。实际上,您所需要做的就是为整个listview定义一种常规颜色(比如黑色),然后在
标签上有color=“White”
,只需将其绑定设置为{binding BindingContext.ChangeColorCommand,Source={x:Reference root},Mode=TwoWay}“
所以我不确定自己是否完全理解需要发生什么,但是如果您想更改某件东西的颜色,可以尝试执行以下操作。我确实更新了一些变量名,使之更合适。让我知道这是否正确
加载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的上下文