简体   繁体   English

如何在没有自定义渲染器的情况下更改 Xamarin Forms Cross Platform 中列表视图所选项目的背景颜色

[英]How to change background color of listview selected item in Xamarin Forms Cross Platform without a custom renderer

how can I change the selected color of a list view selected item in Xamarin Forms Cross Platform?如何在 Xamarin Forms Cross Platform 中更改列表视图选定项目的选定颜色?

I was able to change it using the answer in this question How can I change background color of a ListView in Xaml?我能够使用这个问题中的答案更改它如何更改 Xaml 中 ListView 的背景颜色?

but on first appear the selected item is with the default background color (Orange).但第一次出现时,所选项目使用默认背景颜色(橙色)。

Any suggestions?有什么建议?

I wonder why in 2019 (nearly 2020) there isn't an out of the box way to change this basic and common property easly.我想知道为什么在 2019 年(接近 2020 年)没有一种开箱即用的方法来轻松更改此基本和通用属性。

Thanks.谢谢。

The below code works perfectly for me.下面的代码非常适合我。 However, it iterates the entire ItemsSource for resetting the background of the previously selected item.但是,它会迭代整个 ItemsSource 以重置先前选定项目的背景。 You can store it and reset it if you wish to optimize it.如果您想优化它,您可以存储它并重置它。 Hope this helps.希望这可以帮助。

<ListView x:Name="contactList" ItemsSource="{Binding PlatformsList}" ItemTapped="contactList_ItemTapped"
             VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label TextColor="Black" Margin="10,0" Text="{Binding PlatformName}" BackgroundColor="{Binding Background}" VerticalOptions="Center" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

private void contactList_ItemTapped(object sender, ItemTappedEventArgs e)
{
    var selectedItem = e.Item as PlatformInfo;
    selectedItem.ItemBackground = Color.Aqua;

    foreach(var item in this.contactList.ItemsSource)
    {
        if (item != selectedItem)
            (item as PlatformInfo).ItemBackground = Color.Transparent;
    }
}

I chose to avoid using the ListViews's ItemSelected event or custom renderer approach and apply style changes in the code behind file.我选择避免使用 ListViews 的 ItemSelected 事件或自定义渲染器方法,并在代码隐藏文件中应用样式更改。 The Tapped gesture event and a list (SelectedItems) for tracking the views contained within the active list item, provides easy color style changes. Tapped 手势事件和用于跟踪活动列表项中包含的视图的列表 (SelectedItems) 提供了简单的颜色样式更改。

.xaml file .xaml 文件

<ListView ItemsSource="{Binding ViewModelList}" SelectionMode="None">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions> 
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>                                   
                                <Label Grid.Row="0" Grid.Column="0" Text="{Binding CNumber'}">
                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="Tapped_Command" CommandParameter="{Binding CNumber}" />
                                    </Label.GestureRecognizers>
                                </Label>
                                <Label Grid.Row="0" Grid.Column="1" Text="{Binding Brand}" TextColor="{StaticResource Blue}">
                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="Tapped_Command" CommandParameter="{Binding CNumber}" />
                                    </Label.GestureRecognizers>
                                </Label>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

.xaml.cs code behind file .xaml.cs 文件背后的代码

 private List<View> SelectedItems { get; set; }

    public MYListPage()
    {
        InitializeComponent();           
        BindingContext = new MyViewModel();
        SelectedItems = new List<View>();
    }


       private async void Tapped_Command(object sender, EventArgs e)
    {
        var l = (Label)sender;
        var cNumber= (((TappedEventArgs)e).Parameter) as string;
        if (string.IsNullOrEmpty(contract))
            await DisplayAlert("Error", "Could not load the information", "close");
        else
        {
            if (SelectedItems.Count > 0)
            {
                foreach (var v in SelectedItems)  //reset the styling to the original 'unselected' colors
                {
                    if (v.GetType() == typeof(Grid))
                        ((Grid) v).BackgroundColor = Color.Transparent;
                    else if(v.GetType() == typeof(Label))
                        ((Label)v).TextColor = Color.Black;
                }
                SelectedItems.Clear();
            }
            var parent = (Grid)l.Parent;
            parent.BackgroundColor = Color.FromHex("#E0E0E0");   //set the background color
            SelectedItems.Add(parent);
            foreach (var c in parent.Children)   //option to set additional styling to the child elements
            {
                var child = (Label)c;
                child.TextColor = Color.FromRgb(0, 123, 255);
                SelectedItems.Add(child);
            }
            await Navigation.PushAsync(new MyDetailsPage(viewModel, cNumber));
        }
    }

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

相关问题 xamarin形式:无法更改ListView上所选项目的背景颜色 - xamarin forms: Couldn't change the background color of selected item on ListView 更改 Xamarin Forms 上 ListView 项的背景颜色 - Change the Background Color of an ListView Item on Xamarin Forms 如何更改listview所选项目的文本颜色Xamarin.forms - How to change the listview selected item text color Xamarin.forms 在 Xamarin 中更改 UWP Listview 所选项目背景颜色 - Change UWP Listview selected item background color in Xamarin 如何在Xamarin表单的ListView中更改TextCell的背景颜色? - How to change the background color of `TextCell` inside `ListView` in Xamarin Forms? 根据项C#Xamarin.Forms的值更改ListView项背景颜色 - Change ListView Item Background Color based on the value of the Item C# Xamarin.Forms 如何使用自定义渲染器更改TableSection文本颜色 - Xamarin.Forms C# - How to change the TableSection text color with a custom renderer - Xamarin.Forms C# Xamarin.Forms ListView与CustomViewCell禁用选定的项目颜色 - Xamarin.Forms ListView with CustomViewCell disable Selected Item Color Xamarin.Forms iOS listview所选项目的颜色 - Xamarin.Forms iOS listview selected item color 如何在 Xamarin 表单 NavigationPage 中更改背景颜色 - How to change backGround color in Xamarin forms NavigationPage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM