Tanvir Hossen Rihab
Tanvir Hossen Rihab

Reputation: 1

Changing Label which is nested inside a CollectionView

In my RecentProductsCV CollectionView, I have two <Label>s named PPriceLabel and PLastPriceLabel:

<CollectionView x:Name="RecentProductsCv" SelectionMode="Single">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" Span="2"/>
    </CollectionView.ItemsLayout>
    <CollectionView.EmptyView>
        <Label Text="No Product found." HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
    </CollectionView.EmptyView>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Frame CornerRadius="10" HeightRequest="90" WidthRequest="90" Grid.Row="0">
                    <Image Source="{Binding ProductImage}" Aspect="AspectFit" HeightRequest="90" WidthRequest="90"/>
                </Frame>
                <Label Text="{Binding ProductName}" TextColor="Black" FontSize="Subtitle" Grid.Row="1"/>
                <Label x:Name="PPriceLabel" Text="{Binding ProductPrice, StringFormat='BDT {0}'}" TextColor="#e67e22" FontSize="Caption" Grid.Row="2"/>
                <Label x:Name="PLastPriceLabel" Text="{Binding ProductLastPrice, StringFormat='BDT {0}'}" TextDecorations="Strikethrough" FontSize="Micro" Grid.Row="3"/>
                <StackLayout Orientation="Horizontal" Grid.Row="4">
                    <Label Text="{Binding ProductRatings, StringFormat='({0}/5)'}" TextColor="LightGray" FontSize="Caption"/>
                    <Image Source="ratingStar.png" Aspect="AspectFit" HeightRequest="25" WidthRequest="25"/>
                </StackLayout>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

I want to disable PLastPriceLabel if the values of PPriceLabel and PLastPriceLabel are the same.

Upvotes: 0

Views: 414

Answers (2)

nevermore
nevermore

Reputation: 15786

In your ViewModel, you can add a new property to control whether the PLastPriceLabel is visible or not:

public class myViewModel
{
    public bool isAvailable { get; set; }
    public string ProductPrice { get; set; }
    public string ProductLastPrice { get; set; }

    public myViewModel()
    {
        isAvailable = true;
        getData();
    }

    void getData()
    {
        if (ProductPrice == ProductLastPrice)
        {
            isAvailable = false;
        }
    }
}

In your collectionView, bind the isAvailable to the isVisible property in the Xaml:

<Label x:Name="PLastPriceLabel" IsVisible="{Binding isAvailable}" Text="{Binding ProductLastPrice, StringFormat='BDT {0}'}" TextDecorations="Strikethrough" FontSize="Micro" Grid.Row="3"/>

Then PLastPriceLabel will not be visible when PPriceLabel & PLastPriceLabel value is the same.

Upvotes: 1

Jason
Jason

Reputation: 89082

Xamarin Community Toolkit has a NotEqualConverter you can use to do this

<Label Text="{Binding ProductLastPrice, StringFormat='BDT {0}'}"
       TextDecorations="Strikethrough" FontSize="Micro" Grid.Row="3"
       IsVisible="{Binding ProductLastPrice, Converter={StaticResource NotEqualConverter}, 
       ConverterParameter={Binding ProductPrice}}" />


                    

Upvotes: 0

Related Questions