【发布时间】:2017-04-18 10:23:55
【问题描述】:
我以前检查过一些答案,但没有任何帮助。我正在尝试通过绑定 StackLayout BackgroundColor 来更改 ListView 中 ViewCell 的背景颜色。 现在看起来像这样
每个单元格都应该用不同的颜色填充,但它根本不会改变。背后的代码:
OrderDatapage.XAML:
<ContentPage.Resources>
<ResourceDictionary>
<local:BackgroundConverter x:Key="BackgroundConverter" />
</ResourceDictionary>
</ContentPage.Resources>
.
.
.
.
<StackLayout Orientation="Vertical">
<ListView x:Name="timetableList"
RowHeight="25"
SeparatorVisibility="Default"
Margin="0,0,0,10"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
VerticalOptions="FillAndExpand"
BackgroundColor="{Binding Paint, Converter={StaticResource BackgroundConverter}}">
<Label Text="{Binding Number}"
FontSize="Medium"
Margin="20,0,0,0"
TextColor="White"
BackgroundColor="Black"
/>
<Label Text="{Binding Title}"
FontSize="Default"
Margin="20,0,0,0"
TextColor="Black"
/>
<Label Text="{Binding Date}"
FontSize="Default"
HorizontalOptions="EndAndExpand"
Margin="0,0,40,0"
TextColor="Black"
/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
转换器.cs
class BackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Color.FromHex(value.ToString());
}
}
OrderDataPage.cs
List<TimetableItem> timeTableList { get; set; }
public OrderDataPage()
{
InitializeComponent();
timeTableList = new List<TimetableItem>();
var timetable1 = new TimetableItem() { Number = "1", Title = "Test1", Date = "20-12-2020", Paint = "#63FF20" };
var timetable2 = new TimetableItem() { Number = "2", Title = "Test2", Date = "20-12-2020", Paint = "#FFD933" };
var timetable3 = new TimetableItem() { Number = "3", Title = "Test3", Date = "20-12-2020", Paint = "#C0C0C0" };
timeTableList.Add(timetable1);
timeTableList.Add(timetable2);
timeTableList.Add(timetable3);
timetableList.ItemsSource = timeTableList;
}
【问题讨论】:
-
您的代码看起来不错,您是否检查过断点或者 Value convert 被调用?
标签: c# listview xamarin xamarin.forms converter