【发布时间】:2021-01-16 08:48:33
【问题描述】:
在我的RecentProductsCV CollectionView 中,我有两个<Label>,分别名为PPriceLabel 和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>
如果PPriceLabel 和PLastPriceLabel 的值相同,我想禁用PLastPriceLabel。
【问题讨论】:
-
在模型中创建一个 bool 属性,如果两个值相同则返回 false,并使用它来设置标签的可见性
-
是的,基于此您可以绑定 PLastPriceLabel 的 IsVisible 属性。另一种解决方案也是绑定 PLastPriceLabel 的 IsVisible 属性,但不是与模型中的布尔值绑定,而是使用 IValueConverter 和 PPriceLabel 的值作为参数。
-
@Jason 感谢您的建议,但我对此有点陌生。你能帮我看看演示代码吗?
标签: c# xaml xamarin.forms