进入ListView元素

问题描述 投票:1回答:3

我试图通过ListView循环,因为我想根据对象的内容将“ x:Name =” lblEstatus“设置为不同的颜色。如何访问列表视图的datatemplate元素以更改颜色?] >

  foreach (var item in myLista.ItemsSource)
   {
     //I loop the list with this, but how to access each viewcell ?
     // I tried to make this: to get the view but not possible
     // (ViewCell) test = ... 
   }

//这是我的列表视图的XAML代码

        <ListView x:Name="myLista" HasUnevenRows="true" Margin="0,5,0,5"  SeparatorColor="LightGray"  VerticalOptions="FillAndExpand" RefreshControlColor="#30AAEA">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Tapped="ViewCell_Tapped">
                        <StackLayout Orientation="Vertical" HeightRequest="105">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                <Label Text="EFO: " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15" x:Name="lblClienProve"/>
                                <Label Text="{Binding sRFC}" HorizontalOptions="Center" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15"/>
                                <Label x:Name="lblEstatus" Text="|  Estatus:" HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/> // change this color depending object value
                                <Label Text="{Binding sEstatus}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15" Margin="5,0,5,0"/>
                            </StackLayout>
                            <BoxView  HeightRequest="2" WidthRequest="250" Color="LightSlateGray" HorizontalOptions="Center"/>
                            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                <Label Text="Contribuyente: " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                <Label Text="{Binding sContribuyente}" HorizontalOptions="Center" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15"/>
                            </StackLayout>

                            <StackLayout Spacing="50" HorizontalOptions="Center">
                                <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                    <Label Text="{Binding sNotifica}" HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                    <Label Text=" |  " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                    <Label Text="{Binding sLeido}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15" Margin="5,0,5,0"/>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

我试图通过ListView循环,因为我想根据对象的内容将“ x:Name =” lblEstatus“设置为不同的颜色。如何将列表视图的datatemplate元素访问到...]

c# android listview xamarin xamarin.forms
3个回答
2
投票

您无法通过ItemsSource访问单元格-那里只有您要在单元格中显示的项目。要做的最惯用的方法是将颜色绑定到商品中的某些东西。

您可以通过在商品上设置一个属性来返回正确的颜色,或者通过实现自己的值转换器来做到这一点。转换器可能看起来像这样:


0
投票

关于按项列出foreach的信息,但是您可以根据标签文本来更改颜色

<Label Text="{Binding sEstatus}">
 <Label.Triggers>
  <DataTrigger TargetType="Label" Binding="{Binding sEstatus}" Value="STATUS1">
   <Setter Property="TextColor" Value="Green"/>
  </DataTrigger>
  <DataTrigger TargetType="Label" Binding="{Binding sEstatus}" Value="STATUS2">
   <Setter Property="TextColor" Value="Red"/>
  </DataTrigger>  
 </Label.Triggers>
</Label>

0
投票

首先,在ListView中为项目设置x:Name是一个坏主意,它将无法正常工作。

第二,因为您必须根据内容设置标签的颜色。有两种方法可以做到这一点:

© www.soinside.com 2019 - 2024. All rights reserved.