xamarin 更改CollectionView中所选项目的背景色不适用于UWP

nqwrtyyt  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(79)

我想更改所选项目的背景色。我尝试了以下解决方案,但它不起作用。

<CollectionView x:Name="FlexCategoryType"
                Margin="0"
                HorizontalOptions="FillAndExpand"
                ItemsSource="{Binding ItemsCategory}"
                SelectedItem="{Binding SelectedCategory, Mode=TwoWay}"
                SelectionChanged="FlexCategoryType_SelectionChanged"
                SelectionMode="Single">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout>
                <Label Text="{Binding Name}" />
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="Normal" />
                        <VisualState Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="Yellow" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

CollectionView SelectedItem is not highlighted in Xamarin Forms

2nc8po8w

2nc8po8w1#

更改CollectionView中所选项目的背景色不适用于UWP
问题是你没有特定的CollectionViewSelectionMode,默认值是none。请设置为Single。然后添加VisualStateGroups,如下所示

<CollectionView SelectionMode="Single">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout Margin="10">
                    <Label Text="{Binding}" VerticalOptions="Center" />
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal" />
                            <VisualState Name="Selected">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="Yellow" />
                                </VisualState.Setters>
                            </VisualState>
    
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
        <CollectionView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>1</x:String>
                <x:String>2</x:String>
                <x:String>3</x:String>
                <x:String>4</x:String>
                <x:String>5</x:String>
            </x:Array>
        </CollectionView.ItemsSource>
    </CollectionView>

更新

如何更改ListView单元格选定的颜色。
这里是FormsListViewItem的样式,请将其复制到UWP App.xaml文件中,并根据您想要的颜色编辑SelectedBackground

例如

<Application.Resources>
        <ResourceDictionary>
            <Style x:Key="FormsListViewItem" TargetType="ListViewItem">
                <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
                <Setter Property="TabNavigation" Value="Local" />
                <Setter Property="IsHoldingEnabled" Value="True" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}" />
                <Setter Property="MinHeight" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <ListViewItemPresenter
                        CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
                        ContentMargin="{TemplateBinding Padding}"
                        CheckMode="Inline"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
                        DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
                        DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                        DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
                        DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                        FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
                        FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                        PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
                        PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                        PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                        PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                        ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                        SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
                        SelectionCheckMarkVisualEnabled="True"
                        SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
                        SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
                        SelectedBackground="SeaGreen"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>

相关问题