![](/img/trans.png)
[英]Xamarin Form: Unable to see CarouselView after update the Xamarin.Forms
[英]Xamarin.Forms CarouselView: lower opacity of unselected items
我有一个 Xamarin.Forms Vertical CarouselView,其中 PeakAreaInsets 设置得非常高,所以我可以一次看到列表中的多个项目。 我想要一种能够直观地显示列表中当前“聚焦”或选中的项目的方法。 有没有办法在滚动时动态更改轮播视图中项目的不透明度,以显示当前选择了哪个?
如果有帮助,这是我的代码片段:
<CarouselView ItemsSource="{Binding Days}"
CurrentItem="{Binding SelectedDay}"
VerticalOptions="Center"
HorizontalOptions="Center"
PeekAreaInsets="300"
x:Name="DayCarousel">
<CarouselView.ItemsLayout>
<LinearItemsLayout SnapPointsAlignment="Center"
SnapPointsType="Mandatory"
Orientation="Vertical"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout Spacing="0"
Orientation="Vertical"
HorizontalOptions="Center"
VerticalOptions="Center"
Margin="30,10">
<Label Text="{Binding FormattedDate}"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{StaticResource LabelStyle}"/>
<Label Text="{Binding TitleText}"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{StaticResource LabelHeader1Style}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.SelectDay, Source={x:Reference this}}"
CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
这是我正在寻找的内容的说明:
感谢@MouseOnMars
这是完成这项工作的 xaml。
<CarouselView ItemsSource="{Binding Days}"
CurrentItem="{Binding SelectedDay}"
VerticalOptions="Center"
HorizontalOptions="Center"
PeekAreaInsets="300"
x:Name="DayCarousel">
<CarouselView.ItemsLayout>
<LinearItemsLayout SnapPointsAlignment="Center"
SnapPointsType="Mandatory"
Orientation="Vertical"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout Spacing="0"
Orientation="Vertical"
HorizontalOptions="Center"
VerticalOptions="Center"
Margin="30,10"
Opacity=".25">
<StackLayout.Triggers>
<DataTrigger TargetType="StackLayout"
Binding="{Binding IsSelected}"
Value="True">
<Setter Property="Opacity"
Value="1"/>
</DataTrigger>
</StackLayout.Triggers>
<Label Text="{Binding FormattedDate}"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{StaticResource LabelStyle}"/>
<Label Text="{Binding TitleText}"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{StaticResource LabelHeader1Style}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.SelectDay, Source={x:Reference this}}"
CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
我必须向 ItemTemplate 绑定到的模型添加一个新的 bool 属性“IsSelected”。 然后我不得不在我的页面 ViewModel 的“SelectedDay”属性中添加一些逻辑来打开和关闭 IsSelected bool。 此外,由于我将 CarouselView 直接绑定到模型,因此无论何时打开或关闭,我都必须在模型上为 IsSelected 提高属性 Changed。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.