无法更改 Xamarin.Forms ListView 中所选项目的背景颜色

问题描述 投票:0回答:1

我在更改 Xamarin.Forms 应用程序中所选项目的背景颜色时遇到问题。尽管我付出了努力,背景颜色并没有按预期改变,而且默认的橙色突出显示似乎是硬编码在 Xamarin.Forms 项目深处的某个地方。

我们尝试更改 Xamarin.Forms ListView 中选定框架的背景颜色。具体来说,我们的目标是使用数据绑定和 BooleanToColorConverter 根据项目的选择状态动态更新背景颜色。然而,尽管我们做出了努力,所选框架仍然默认为橙色突出显示,这似乎是由 Xamarin.Forms 框架内的某些基础样式或默认行为设置的。

橙色按钮

起源 xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MentalTest.Views"
             x:Class="MentalTest.Views.NewSurveyPage"
             Title="{Binding CurrentQuestion.QuestionText}"
             NavigationPage.HasNavigationBar="False">
    <ContentPage.Content>
        <AbsoluteLayout>
            <!-- Background shapes -->
            <Image Source="VectorBlue.png"
                   AbsoluteLayout.LayoutBounds="0.5, 1.2, 1, 1"
                   AbsoluteLayout.LayoutFlags="All"
                   Aspect="AspectFill" />
            <Image Source="VectorOrange.png"
                   AbsoluteLayout.LayoutBounds="0.5, 1.5, 1, 1"
                   AbsoluteLayout.LayoutFlags="All"
                   Aspect="AspectFill" />

            <!-- Content -->
            <Grid AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <!-- Header -->
                <StackLayout Orientation="Horizontal"
                             BackgroundColor="White"
                             HeightRequest="50"
                             Padding="10"
                             VerticalOptions="Start"
                             Grid.Row="0">
                    <ImageButton Source="back_arrow.png"
                                 HorizontalOptions="Start"
                                 VerticalOptions="Center"
                                 WidthRequest="36"
                                 HeightRequest="36"
                                 BackgroundColor="Transparent"
                                 Rotation="180"
                                 Clicked="OnBackButtonClicked" />
                    <Label Text="Back to Test Cards"
                           FontSize="24"
                           FontAttributes="Bold"
                           HorizontalOptions="Start"
                           VerticalOptions="Center"
                           TextColor="#394758"
                           Margin="10,0,0,0" />
                </StackLayout>

                <!-- Content -->
                <StackLayout Padding="20"
                             VerticalOptions="FillAndExpand"
                             Grid.Row="1">
                    <Label Text="{Binding QuestionCounterText}"
                           FontSize="16"
                           TextColor="#394758"
                           HorizontalOptions="Center" />

                    <!-- Question and Answers -->
                    <StackLayout Spacing="20"
                                 VerticalOptions="FillAndExpand">
                        <Label Text="{Binding CurrentQuestion.QuestionText}"
                               FontSize="20"
                               FontAttributes="Bold"
                               HorizontalOptions="Center" 
                               VerticalOptions="Start"
                               TextColor="#394758"
                               Margin="0,20,0,20" />

                        <ListView ItemsSource="{Binding CurrentAnswers}"
                                  SelectedItem="{Binding SelectedAnswer}"
                                  SeparatorVisibility="None"
                                  BackgroundColor="Transparent"
                                  VerticalOptions="FillAndExpand"
                                  Margin="0,20,0,0">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" Padding="0">
                                            <Frame BorderColor="#b76e79"
                                                   CornerRadius="30"
                                                   Padding="6"
                                                   Margin="10,10,10,15"
                                                   BackgroundColor="White"
                                                   HasShadow="True"
                                                   HeightRequest="100"
                                                   VerticalOptions="CenterAndExpand">
                                                <Label Text="{Binding .}"
                                                       FontSize="16"
                                                       TextColor="#394758"
                                                       VerticalTextAlignment="Center"
                                                       HorizontalOptions="CenterAndExpand"/>
                                            </Frame>
                                        </StackLayout>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </StackLayout>
                </StackLayout>

                <!-- Continue Button -->
                <Button Command="{Binding ContinueCommand}" 
                        Text="Continue" 
                        HorizontalOptions="FillAndExpand"
                        VerticalOptions="End"
                        HeightRequest="50"
                        Margin="20,10"
                        BackgroundColor="#4A90E2"
                        TextColor="White"
                        CornerRadius="25"
                        Grid.Row="2"
                        IsEnabled="{Binding SelectedAnswer}" />
            </Grid>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

c# xaml xamarin xamarin.forms xamarin.android
1个回答
0
投票

解决此问题的一个简单方法是在 ListView 中将 SelectionMode 设置为 None

<ListView SelectionMode="None" ..../>
© www.soinside.com 2019 - 2024. All rights reserved.