简体   繁体   English

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

[英]Change ListView Item Background Color based on the value of the Item C# Xamarin.Forms

I tried looking up the similar problem on the Internet but nothing seems to be like mine. 我尝试在互联网上查找类似的问题,但似乎没有什么比我的。

I have a ListView that displays list of records: 我有一个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>

I need to change ItemsBackground BoxView 's Color based on the result of comparison of two Label s Arrivo and Partenza . 我需要根据两个LabelArrivoPartenza的比较结果来更改ItemsBackground BoxView的颜色。 Basically, on the page there is DatePicker named MainDatePicker . 基本上,页面上有一个名为MainDatePicker DatePicker I want to compare the records' Arrivo and Partenza Date Values with the MainDatePicker.Date . 我想将记录的ArrivoPartenza日期值与MainDatePicker.Date If Arrivo == MainDatePicker.Date I want to change the ItemsBackground BoxView 's Color to Green. 如果Arrivo == MainDatePicker.Date我想将ItemsBackground BoxView的颜色更改为绿色。 If Partenza matches the MainDatePicker.Date the ItemsBackground BoxView 's Color should be Red. 如果PartenzaMainDatePicker.Date匹配,则ItemsBackground BoxView的颜色应为红色。

If none of the above is true, the color remains white. 如果以上都不是,则颜色保持白色。

Is it possible to achieve this? 有可能实现这一目标吗?

you can set in side listViewBox colour like this ` 您可以像这样在侧面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>

` `

Add new property in your SelectedTavoloItem, named for example color and when you initialize data source for TavoloListView just verify your condition 在您的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;
}

and when you bind list add this code in your box for background: 并在绑定列表时,将以下代码添加到背景框中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM