Reputation: 73
Im trying to loop trhough a ListView, because I want to set "x:Name="lblEstatus"in different color depending on the object content. How can I access to the datatemplate elements of my list view to change the color ?
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 = ...
}
// This is my XAML code for the list view
<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>
Upvotes: 1
Views: 877
Reputation: 1136
First of all,setting x:Name for items inside ListView is a bad idea and it won't work.
Secondly , as you have to set color of labels depending on content.There are 2 ways to do that:
1: IValueConverter: Based on content,you can return TextColor.
2: Create a new Property in your Model naming TextColorProperty
and add proper logic to update this property for every object assigned.
Then bind it to TextColor property as TextColor={Binding TextColorProperty}
Upvotes: 0
Reputation: 555
Idk about foreach by items source, but you can do this for change color based on the label text
<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>
Upvotes: 0
Reputation: 4658
You cannot access the cells via the ItemsSource
- there you only have the items that you are displaying in your cells. The most idiomatic way to do what you want to do is to bind the color to something in your item.
You can either do that by having a property on your item that returns the correct color or by implementing your own value converter. A converter could look somewhat like this:
public class TextToColorConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value switch
{
"value1" => Color.Black,
"value2" => Color.Red,
_ => Color.Blue
};
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//
}
}
You can read more about IValueConverter
on learn.microsoft.com
Upvotes: 2