XAML CollectionView自定义渲染器:选定项目背景颜色不起作用

zzzyeukh  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(92)

我正在使用Xamarin.Forms,并且创建了一个CollectionView自定义渲染器控件,因为我需要在某些情况下禁用滚动,但是该控件似乎不会自动更改所选项目的背景颜色。如果我使用CollectionView,则可以为选定项着色,但在自定义控件中则不行。
我在特定于平台的资源文件中设置全局样式:

<style name="MainTheme" parent="MainTheme.Base">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorPrimaryDark">#00FF00</item>
<item name="android:colorPressedHighlight">@color/ListViewSelected</item>
<item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
<item name="android:colorFocusedHighlight">@color/ListViewSelected</item>
<item name="android:colorActivatedHighlight">@color/ListViewSelected</item>
<item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item>
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>

字符串
下面是自定义渲染器:

[assembly: ExportRenderer(typeof(CollectionViewExt), typeof(CollectionViewCustomRenderer))]
namespace Vendo.Android.Renderers
{
 internal class CollectionViewCustomRenderer: CollectionViewRenderer
 {
     public CollectionViewCustomRenderer(Context context) : base(context)
     {
     }

     public override bool OnInterceptTouchEvent(MotionEvent ev)
     {
         if (Element is CollectionViewExt collectionView && collectionView.IsScrollEnabled)
             return base.OnInterceptTouchEvent(ev);
         else
             return false;
     }

     public override bool OnTouchEvent(MotionEvent ev)
     {
         if (Element is CollectionViewExt collectionView && collectionView.IsScrollEnabled)
             return base.OnTouchEvent(ev);
         else
             return false;
     }
 }
}


按照xaml页面代码:

<controls:CollectionViewExt x:Name="cvArticoli" 
                        VerticalScrollBarVisibility="Always"   
                        VerticalOptions="FillAndExpand"
                        IsScrollEnabled="{Binding IsScrollEnabled}"
                        Style="{StaticResource CollExtStyle}"
                        ItemsSource="{Binding ListaArticoli}" 
                        SelectedItem="{Binding ArticoloModelCorrente, Mode=TwoWay}"
                        IsVisible="{Binding VisualizzaArticoli}"
                        ClassId="{Binding CatalogoVisualizzato}"
                        ItemTemplate="{StaticResource ArticoliTemplateSelector}"
                        RemainingItemsThreshold="2" 
                        RemainingItemsThresholdReachedCommand="{Binding LoadMoreArticoliCommand}">

            <controls:CollectionViewExt.EmptyView>
                <StackLayout>
                    <Label 
                        Margin="0,10"                            
                        HorizontalOptions="Center"
                        Text="Nessun articolo in lista."
                        TextColor="Gray"/>
                </StackLayout>
            </controls:CollectionViewExt.EmptyView>

            <controls:CollectionViewExt.ItemsLayout>
                <MultiBinding>
                    <Binding Path="CatalogoVisualizzato"/>
                    <Binding Path="ColonneCatalogo"/>

                    <MultiBinding.Converter>
                        <converters:BoolToItemsLayoutConverter/>
                    </MultiBinding.Converter>
                </MultiBinding>
            </controls:CollectionViewExt.ItemsLayout>
        </controls:CollectionViewExt>


以下是collectionView Visual State Manager的app.xaml:

<Style x:Key="CollExtStyle" TargetType="controls:CollectionViewExt">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="White" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="{StaticResource Highlight}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="{StaticResource Disabled}"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>

r3i60tvu

r3i60tvu1#

解决办法很简单。只需设置CollectionView的SelectionMode属性,如下所示:

<controls:CollectionViewExt x:Name="cvArticoli" 
            VerticalOptions="FillAndExpand"
            SelectionMode="Single"
            Style="{StaticResource CollExtStyle}"
            IsScrollEnabled="{Binding IsScrollEnabled}"
            ItemsSource="{Binding ListaArticoli}" 
            SelectedItem="{Binding ArticoloModelCorrente, Mode=TwoWay}"
            IsVisible="{Binding VisualizzaArticoli}"
            ClassId="{Binding CatalogoVisualizzato}"
            ItemTemplate="{StaticResource ArticoliTemplateSelector}"                                                                                
            RemainingItemsThreshold="2" 
            RemainingItemsThresholdReachedCommand="{Binding LoadMoreArticoliCommand}">

字符串

相关问题