【问题标题】:Changing Label which is nested inside a CollectionView更改嵌套在 CollectionView 中的标签
【发布时间】:2021-01-16 08:48:33
【问题描述】:

在我的RecentProductsCV CollectionView 中,我有两个<Label>,分别名为PPriceLabelPLastPriceLabel

<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>

如果PPriceLabelPLastPriceLabel 的值相同,我想禁用PLastPriceLabel

【问题讨论】:

  • 在模型中创建一个 bool 属性,如果两个值相同则返回 false,并使用它来设置标签的可见性
  • 是的,基于此您可以绑定 PLastPriceLabel 的 IsVisible 属性。另一种解决方案也是绑定 PLastPriceLabel 的 IsVisible 属性,但不是与模型中的布尔值绑定,而是使用 IValueConverter 和 PPriceLabel 的值作为参数。
  • @Jason 感谢您的建议,但我对此有点陌生。你能帮我看看演示代码吗?

标签: c# xaml xamarin.forms


【解决方案1】:

在您的 ViewModel 中,您可以添加一个新属性来控制 PLastPriceLabel 是否可见:

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;
        }
    }
}

在您的 collectionView 中,将 isAvailable 绑定到 Xaml 中的 isVisible 属性:

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

PPriceLabel & PLastPriceLabel 值相同时,PLastPriceLabel 将不可见。

【讨论】:

    【解决方案2】:

    Xamarin Community Toolkit 有一个 NotEqualConverter,您可以使用它来执行此操作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多