根据项C#Xamarin.Forms的值更改ListView项背景颜色

时间:2018-08-05 22:20:32

标签: c# listview xamarin.forms binding conditional-formatting

我尝试在Internet上查找类似的问题,但似乎没有什么像我的。

我有一个ListView显示记录列表:

    <ListView x:Name="TavoloListView" HasUnevenRows="true" Grid.Row="2" SeparatorColor="Black"  
     SelectedItem="{Binding SelectedTavoloItem, Mode=TwoWay}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid  ColumnSpacing="0" RowSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />

                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <BoxView x:Name="ItemsBackground" Grid.Column="0" Grid.ColumnSpan="6" Color="White" Opacity="0.5"/>
                        <Label Text="{Binding TavoloNo}" Grid.Row="0" Grid.Column="0" TextColor="Black" HorizontalOptions="Center"/>
                        <Label Text="{Binding CameraNo}" Grid.Row="0" Grid.Column="1" TextColor="Black" HorizontalOptions="Center"/>
                        <Label Text="{Binding Arrivo, StringFormat='\{0:dd/MM/yy}'}" Grid.Row="0" Grid.Column="2" TextColor="Black" HorizontalOptions="Center"/>
                        <Label Text="{Binding Partenza, StringFormat='\{0:dd/MM/yy}'}" Grid.Row="0" Grid.Column="3" TextColor="Black" HorizontalOptions="Center"/>
                        <Label Text="{Binding PersoneTot}" Grid.Row="0" Grid.Column="4" TextColor="Black" HorizontalOptions="Center"/>
                        <Label Text="{Binding Bambini, StringFormat='(\{0}\)'}" Grid.Row="0" Grid.Column="5" TextColor="Black" HorizontalOptions="Center"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我需要根据两个ItemsBackgroundBoxView的比较结果来更改Label Arrivo的颜色。 基本上,页面上有名为Partenza的{​​{1}}。 我想将记录的DatePickerMainDatePicker日期值与Arrivo进行比较。如果Partenza我想将MainDatePicker.Date Arrivo == MainDatePicker.Date的颜色更改为绿色。如果ItemsBackgroundBoxView相匹配,则Partenza MainDatePicker.Date的颜色应为红色。

如果以上所有条件都不成立,则颜色保持白色。

有可能实现这一目标吗?

2 个答案:

答案 0 :(得分:1)

您可以像这样在侧面listViewBox中设置颜色 `

<ListView Grid.Row="1" ItemsSource="{Binding BindListViewData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <BoxView>
                                    <BoxView.Triggers>
                                        <DataTrigger TargetType="BoxView" Binding="{Binding BindBoolValueFromModel}" Value="True">
                                            <Setter Property="BackgroundColor" Value="Green" />
                                        </DataTrigger>
                                        <DataTrigger TargetType="BoxView" Binding="{Binding BindBoolValueFromModel}" Value="False">
                                            <Setter Property="BackgroundColor" Value="Red" />
                                        </DataTrigger>
                                    </BoxView.Triggers>
                                </BoxView>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                </ListView>

`

答案 1 :(得分:0)

在您的SelectedTavoloItem中添加新属性,例如以color命名,并且当您初始化TavoloListView的数据源时,只需验证条件

public void UpdateDataSourceForLIst()
{

List<SelectedTavoloItem> newListData=new List<SelectedTavoloItem>();
foreach(var item in DataSourceForList)
{
    if(Arrivo == MainDatePicker.Date)
    {
      item.color=green;
      newListData.Add(item);
    }else{
      item.color=red;
      newListData.Add(item);
    }

 }
DataSourceForList=newListData;
}

当您绑定列表时,请在框中输入以下代码作为背景:

`<BoxView 
   x:Name="ItemsBackground" 
   Grid.Column="0" 
   Grid.ColumnSpan="6" 
   Color="{Binding color}" ////  bind new created property 
   Opacity="0.5"/>`