本文介绍了Xamarin表单中未突出显示CollectionView SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试突出显示Xamarin表单 CollectionView 项目,但似乎没有突出显示.在调试时,我发现如果删除 SwipeView Gesture 事件,该事件将按预期运行.此外,在当前代码中,选择项已更改,但颜色未更改.如何突出显示所选项目?

I am trying to highlight Xamarin Forms CollectionView Item, but it seems it doesn't highlight. While debugging I found out that if I remove the SwipeView Gesture event it works as expected. Additionally, in the current code the selection item is changed, but the color is not changning. How can I highlight the selected item?

<CollectionView ItemsSource="{Binding SomeList}"
SelectedItem="{Binding CurrentItem}" SelectionMode="Single" x:Name="itemView">
<CollectionView.ItemsLayout>
     <GridItemsLayout Orientation="Vertical" Span="1"/>
  </CollectionView.ItemsLayout>
  <CollectionView.ItemTemplate>
    <DataTemplate>
    <SwipeView>
     <SwipeView.GestureRecognizers>
         <SwipeGestureRecognizer Direction="Right"
          Command="{Binding BindingContext.SwipGestureCommand, Source={x:Reference itemView}}"
                                                            CommandParameter="right"/>
   </SwipeView.GestureRecognizers>
   <StackLayout Orientation="Vertical" Padding="5">
    <VisualStateManager.VisualStateGroups>
         <VisualStateGroup Name="CommonStates">
        <VisualState Name="Normal" />
        <VisualState Name="Selected">
             <VisualState.Setters>
                     <Setter Property="BackgroundColor" Value="Yellow" />
             </VisualState.Setters>
       </VisualState>
      </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
          <Label LineBreakMode="WordWrap"  Text="{Binding SomeText}"/>
        </StackLayout>
      </SwipeView>
    </DataTemplate>
  </CollectionView.ItemTemplate>
</CollectionView>

如何突出显示所选的收藏夹查看项?

How to highlight the selected collectionview item?

推荐答案

请先将Xamarin.Forms版本升级到4.6及更高版本,然后像下面的代码一样修改代码,将滑动背景颜色设置为白色,并为以下代码添加VisualStateManager.VisualStateGroups:刷卡.

Please upgrade Xamarin.Forms version to 4.6 and above, then modify your code like the following code,setting swip background color as white, and adding VisualStateManager.VisualStateGroups for swip.

 <CollectionView
            x:Name="itemView"
            ItemsSource="{Binding SomeList}"
            SelectionMode="Single">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="1" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView BackgroundColor="White">
                        <SwipeView.GestureRecognizers>
                            <SwipeGestureRecognizer
                                Command="{Binding BindingContext.SwipGestureCommand, Source={x:Reference itemView}}"
                                CommandParameter="right"
                                Direction="Right" />
                        </SwipeView.GestureRecognizers>
                        <StackLayout Padding="5" Orientation="Vertical">
                            <Label LineBreakMode="WordWrap" Text="{Binding SomeText}" />

                        </StackLayout>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">
                                <VisualState Name="Normal" />
                                <VisualState Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor" Value="Yellow" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

这篇关于Xamarin表单中未突出显示CollectionView SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:55