如何修改ListView文本颜色?绑定错误致界面空白求解
解决ListView文本颜色修改及界面空白问题
嘿,我一眼就揪出问题所在啦!
你碰到的界面空白+文本颜色不生效的问题,核心是绑定路径写错了:
- 你的
ListView.ItemsSource绑定的是Attributes(一个ObservableCollection<string>),这意味着列表里每一项的BindingContext都是单个的string对象。 - 但你在
Label.Text里写了{Binding Attributes},单个string对象里根本不存在名为Attributes的属性,所以Label拿不到内容,界面自然就空白了,颜色设置也就无从谈起。
修正方案
只需要把Label的绑定路径改成绑定当前项本身就行,两种写法都可以:
- 写法1:
Text="{Binding .}"(.代表绑定当前BindingContext,也就是单个字符串) - 写法2:
Text="{Binding}"(省略路径时默认绑定当前BindingContext)
修正后的完整XAML代码:
<ListView BackgroundColor="#004B86" ItemsSource="{Binding Attributes}" > <ListView.ItemTemplate> <DataTemplate> <Label Text="{Binding .}" TextColor="White"/> </DataTemplate> </ListView.ItemTemplate> </ListView>
这样修改后,列表内容会正常显示,同时TextColor="White"的设置也会生效,完美解决你的需求~
内容的提问来源于stack exchange,提问作者user14139029
相关产品推荐
相关产品推荐

