簡體   English   中英

根據項C#Xamarin.Forms的值更改ListView項背景顏色

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

我嘗試在互聯網上查找類似的問題,但似乎沒有什么比我的。

我有一個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>

我需要根據兩個LabelArrivoPartenza的比較結果來更改ItemsBackground BoxView的顏色。 基本上,頁面上有一個名為MainDatePicker DatePicker 我想將記錄的ArrivoPartenza日期值與MainDatePicker.Date 如果Arrivo == MainDatePicker.Date我想將ItemsBackground BoxView的顏色更改為綠色。 如果PartenzaMainDatePicker.Date匹配,則ItemsBackground BoxView的顏色應為紅色。

如果以上都不是,則顏色保持白色。

有可能實現這一目標嗎?

您可以像這樣在側面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>

`

在您的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"/>`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM