简体   繁体   English

Xamarin.Forms CarouselView:降低未选择项目的不透明度

[英]Xamarin.Forms CarouselView: lower opacity of unselected items

I have a Xamarin.Forms Vertical CarouselView where the PeakAreaInsets is set pretty high so I can see multiple items in the list at once.我有一个 Xamarin.Forms Vertical CarouselView,其中 PeakAreaInsets 设置得非常高,所以我可以一次看到列表中的多个项目。 I want a way to be able to visually show which item in the list is currently "focused" or selected.我想要一种能够直观地显示列表中当前“聚焦”或选中的项目的方法。 Is there a way to dynamically change the opacity of Items in a Carousel View as you scroll through it, so as to show which one is currently selected?有没有办法在滚动时动态更改轮播视图中项目的不透明度,以显示当前选择了哪个?

Here's a snippet of my code if that helps:如果有帮助,这是我的代码片段:

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

Here is an illustration of what I am looking for:这是我正在寻找的内容的说明:

在此处输入图片说明

Thanks to @MouseOnMars感谢@MouseOnMars

Here is the xaml that made this work.这是完成这项工作的 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>

I had to add a new bool property "IsSelected" to the Model that the ItemTemplate is bound to.我必须向 ItemTemplate 绑定到的模型添加一个新的 bool 属性“IsSelected”。 Then I had to add some logic in my "SelectedDay" Property on my Page ViewModel to turn on and off the IsSelected bool.然后我不得不在我的页面 ViewModel 的“SelectedDay”属性中添加一些逻辑来打开和关闭 IsSelected bool。 Additionally since I am binding the CarouselView directly to a model I had to raise property Changed on the model for IsSelected whenever it was switched on or off.此外,由于我将 CarouselView 直接绑定到模型,因此无论何时打开或关闭,我都必须在模型上为 IsSelected 提高属性 Changed。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM