Background select item of List View

本秂侑毒 提交于 2021-02-16 15:37:13

问题


I have a page.xaml with a listview. The background color of the page is white and when I tap a ListView item, the background of this item is gray. How can I change this color (of the background selected item) in a cross-platform way?

Page.xaml

page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Materie_Universitarie.Diritto_Commerciale
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class indicecommerciale : ContentPage
    {
        public indicecommerciale()
        {
            InitializeComponent();

            argomentilist.ItemsSource = new List<Argomenti>()
           {

                new Argomenti()
                {
                    Capitolo = "La società di persone", Descrizione = ""
                },

                new Argomenti()
                {
                    Capitolo = "Le società di capitali", Descrizione = ""
                },

                new Argomenti()
                {
                    Capitolo = "I gruppi di società", Descrizione = ""
                }
            };
        }

        public class Argomenti: INotifyPropertyChanged
        {
            public string Capitolo
            {
                get;
                set;
            }

            public string Descrizione
            {
                get;
                set;
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private Color _backgroundColor;

            public Color BackgroundColor
            {
                get { return _backgroundColor; }
                set
                {
                    _backgroundColor = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor"));
                    }
                }
            }

            public void SetColors(bool isSelected)
            {
                if (isSelected)
                {
                    BackgroundColor = Color.FromRgb(0.20, 0.20, 1.0);
                }
                else
                {
                    BackgroundColor = Color.FromRgb(0.95, 0.95, 0.95);
                }
            }

        }




        private void argomentilist_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var nme = (Argomenti)e.Item;

            switch (nme.Capitolo
                )
            {
                case "Reati contro la persona":
                    Navigation.PushAsync(new local());
                    break;

                case "Reati contro la bellezza di Bibione":
                    Navigation.PushAsync(new DirittoPenale1());
                    break;
            }

        }
    }


}

I Will change with your hints. Thanks a lot for the time that you have lost for me


回答1:


You can achieve it like following ways In a cross-platform way.

Add this properties in your Model.

    public class Argomenti : INotifyPropertyChanged
{
    public string Capitolo
    {
        get;
        set;
    }

    public string Descrizione
    {
        get;
        set;
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private Color _backgroundColor;

    public Color BackgroundColor
    {
        get { return _backgroundColor; }
        set
        {
            _backgroundColor = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor"));
            }
        }
    }

    public void SetColors(bool isSelected)
    {
        if (isSelected)
        {
            BackgroundColor = Color.FromRgb(0.20, 0.20, 1.0);
        }
        else
        {
            BackgroundColor = Color.FromRgb(0.95, 0.95, 0.95);
        }
    }
}

argomentilist_ItemTapped method.

   private void argomentilist_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        var selectedItem = e.Item as Argomenti;

        selectedItem.SetColors(true);
    }

Layout code.

   <StackLayout>
    <!-- Place new controls here -->
    <StackLayout Margin="10,10,0,0">
        <Label TextColor="Black" Text="Selezionare l'argomento" FontAttributes="Italic"></Label>
        <ListView ItemTapped="argomentilist_ItemTapped" x:Name="argomentilist">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell >
                        <StackLayout Orientation="Vertical" BackgroundColor="{Binding BackgroundColor}" >
                            <Label Text="{Binding Capitolo}" Font="18" TextColor="Black"></Label>
                            <Label Text="{Binding Descrizione}" Font="14" TextColor="Gray"></Label>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</StackLayout>

=======Update============== Please change the Argomenti like following code.

public class Argomenti : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Capitolo
    {
        get;
        set;
    }

    public string Descrizione
    {
        get;
        set;
    }


    Color _backgroundColor;

    public Color BackgroundColor
    {
        get { return _backgroundColor; }
        set
        {
            _backgroundColor = value;


            OnPropertyChanged("BackgroundColor");

        }
    }

    public void SetColors(bool isSelected)
    {
        if (isSelected)
        {
            BackgroundColor = Color.FromRgb(0.20, 0.20, 1.0);
        }
        else
        {
            BackgroundColor = Color.FromRgb(0.95, 0.95, 0.95);
        }
    }
}


来源:https://stackoverflow.com/questions/58621697/background-select-item-of-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!