我在listView中有保留时间,例如8:00、9:00等。通过JSON,我从远程数据库中检索了已经进行的保留。因此,我想将每个单元格(标签)的背景颜色更改为保留的红色,其余部分更改为绿色(免费约会)。
这是我的xaml代码:
<StackLayout>
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label Text="{Binding Text}"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="16" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
ListView是通过以下模型填充的:
new Termin { Id = Guid.NewGuid().ToString(), Text = "12:00", Description="" },
那我该如何改变这些细胞的颜色?
我想要的伪代码:
for(int i=0; i< number of items in the listview; i++) {
if(reservations.contains(listview.itemAt(i)) {
//change background color of viewcell (label?) at position i
}
}
答案 0 :(得分:2)
(如Bruno所说)
IsReserved
布尔属性:public class Termin
{
public string Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public bool IsReserved { get; set; }
}
IValueConverter
如果Red
为true,则返回IsReserved
:public class IsReservedToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value ? Color.Red : Color.Transparent);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xmlns:local="clr-namespace:SameNameSpace;assembly=SomeAssemblyName"
<ContentPage.Resources>
<ResourceDictionary>
<local:IsReservedToColorConverter x:Key="IsReservedToColor"></local:IsReservedToColorConverter>
</ResourceDictionary>
</ContentPage.Resources>
<Frame BackgroundColor = "{Binding IsReserved, Converter={StaticResource IsReservedToColor}}">
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Forms_31_1.ListPage"
xmlns:local="clr-namespace:Forms_31_1;assembly=Forms_31_1" >
<ContentPage.Resources>
<ResourceDictionary>
<local:IsReservedToColorConverter x:Key="IsReservedToColor"></local:IsReservedToColorConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<ListView x:Name="listView" BackgroundColor="Aqua" SeparatorColor="Red">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor = "{Binding IsReserved, Converter={StaticResource IsReservedToColor}}">
<StackLayout Orientation="Vertical">
<Label Text="{Binding Text}" />
<Label Text="{Binding Description}" />
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
posts.Add(new Termin { Id = Guid.NewGuid().ToString(), Text = "11:00" });
posts.Add(new Termin { Id = Guid.NewGuid().ToString(), Text = "12:00", IsReserved = true });
posts.Add(new Termin { Id = Guid.NewGuid().ToString(), Text = "13:00" });
posts.Add(new Termin { Id = Guid.NewGuid().ToString(), Text = "14:00" });
答案 1 :(得分:0)
您可以使用自定义视图单元格。我已经在项目中编写了一个自定义视图单元格,并使用了XFGloss(XFGloss是Xamarin.Forms项目的附加组件,它为标准XF页面和控件类添加了新属性),以使listView的行变得丰富多彩。您的listView不会失去XFGloss的触觉反馈。我使用的自定义viewCell是:
top
及其在xaml文件中的用法很简单,如下行:
top