Modifier un élément d'une liste

Maru_5601 Messages postés 1 Date d'inscription jeudi 9 mai 2019 Statut Membre Dernière intervention 9 mai 2019 - Modifié le 9 mai 2019 à 23:30
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 - 9 mai 2019 à 21:23
Bonjour,

je travaille avec WPF pour l’ajoutes des personnes sur une liste , et j'ai pas pu a enregistrer la modification d'un élément sélectionner

voici mon script :

private bool  isModifier = false;
private ObservableCollection<Personne> _usersList;

        public ObservableCollection<Personne> _UsersList
        {
            get{
                return _usersList; 
            }
            set{  if (_usersList != value){

                _usersList = value;
                NotifyPropertyChanged("_UsersList");
            }
            }
        }

private Personne selectedObject;
        public Personne SelectedObject
        {
            get { return selectedObject; }
            set { selectedObject = value;
                NotifyPropertyChanged("SelectedObject"); 
            }
        }

public void EditAction(object obj)
        {
            // Write My Actions Commands Here
            //Binding text Box property from UserViewModel & Model Classes

            Personne p = (Personne)obj;

            Nom = p.Nom;
            Prenom = p.Prenom;
            DateTime? Date_Inscription = p.Date_Inscription;
            Ville = p.Ville;
            Tel = p.Tel;
            Email = p.Email;
            isModifier = true;

     

        } 
 private ICommand _clickCommand;
        public ICommand ClickCommand{
            get
            {
               if( _clickCommand == null)
               {
                _clickCommand =  new CommandHandler((ob) => MyAction(ob), true);
               }
               return _clickCommand;
            }
        }

 public void MyAction(object ob)
        {
            // Write My Actions Commands Here
            //Binding text Box property from UserViewModel & Model Classes
            if (isModifier == false)
            {
                int lastId = 0;
                if (_UsersList.Count > 0)
                    lastId = _UsersList.Count + 1;
                else
                    lastId = 1;
                _UsersList.Add(new Personne() { Id = lastId, Nom = Nom, Prenom = Prenom, Ville = Ville, Date_Inscription = Date_Inscription, Email = Email, Tel = Tel });

                Nom = string.Empty;
                Prenom = string.Empty;
                Date_Inscription = DateTime.Now;
                Ville = string.Empty;
                Email = string.Empty;
                Tel = string.Empty;

            }
            else
          { 
             SelectedObject.Nom = Nom;
             SelectedObject.Prenom = Prenom;
             SelectedObject.Ville = Ville;
             SelectedObject.Date_Inscription = Date_Inscription;
             SelectedObject.Email = Email;
             SelectedObject.Tel = Tel;
             isModifier = false;


         }


Merci d'avance

1 réponse

vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
9 mai 2019 à 21:23
Bonjour !
Merci d'appliquer ce qui est décrit ici : https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Ton code sera beaucoup plus agréable à lire
Merci
0
Rejoignez-nous