Utiliser deux combobox

Fermé
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021 - 13 mai 2021 à 07:11
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 - 14 mai 2021 à 09:34
Bonjour j'utilise deux combobox l'un pour stocker les années de debut et l'autre pour stocker les années de fin lorsque je selectionne une année de debut dans le premier combobox la valeur minimale du deuxième combobox doit commencer par l'année de debut que l'utilisateur a selectionné dans le premier combobox pouvez m'orienter svp merci
A voir également:

2 réponses

Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 928
13 mai 2021 à 08:51
Bonjour

dans le code behind de ton contrôle, tu vas ajouter 2 propriété AnneeDebut et AnneeFin, elles doivent être INotifyPropertyChanged.

La propriété SelectedItem des combobox sera bindée sur ces 2 propriétés.

Dans le code get de AnneeDebut, tu affecteras AnneeFin, si sa valeur est inférieure à la nouvelle valeur de AnneeDebut.
Dans le code get de AnneeFin, tu affecteras AnneeDebut, si sa valeur est supérieure à la nouvelle valeur de AnneeFin.

Tu peux même créer 2 collections d'années que tu réduits en fonction de la saisie.

0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
13 mai 2021 à 08:53
d'accord je fais ça merci
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
14 mai 2021 à 00:41
j'ai fais mais dans mon deuxième combobox ça affiche juste 0 partout
voila une partie du code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;

using System.Data;
using MySql.Data.MySqlClient;

using DAL;

namespace BLL
{
    /// <summary>
    /// Nom:Daniel Kabeya
    /// Matricule:1812147
    /// </summary>
    public class Vente: INotifyPropertyChanged
    {

        public static ObservableCollection<Vente> vente = new ObservableCollection<Vente>();
        public static ObservableCollection<Vente> ListeanneeDebut = new ObservableCollection<Vente>();
        public static ObservableCollection<Vente> ListeanneeFin = new ObservableCollection<Vente>();

        /* public Vente(string province,string genre,int annee,double nbunites, double montantx1000)
         {
             this.province = province;
             this.genre = genre;
             this.annee = annee;
             this.nbunites = nbunites;
             this.montantx1000 = montantx1000;

         }*/


        /// <summary>
        /// Methode qui est de type "void" qui me permet de récuperer les valeurs de chaque lignes de ma table et ensuite les affecter à mes propriétés à la 
        /// fin j'ajoute cela à ma liste 
        /// </summary>
        public static void ChargerListeVente()
        {
            //Création de la collection observable
            var listeVente = new ObservableCollection<Vente>();
            //Appel de la fonction dans AccessDB pour la connection à la DB
            DataTable dt = AccessDB.ConnecterBDVente();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                var ven = new Vente

                {

                    //Création d'un nouvel objet de type province 
                    LaProvince = new Province(dt.Rows[i]["Province"].ToString()),
                    //Création d'un nouvel objet de type vehicule
                    LesGenres = new Vehicule(dt.Rows[i]["Genre"].ToString()),
                    Annee = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
                    NbUnites = Int32.Parse(dt.Rows[i]["NbUnites"].ToString()),
                    Montantx1000 = Int32.Parse(dt.Rows[i]["Montantx1000"].ToString()),


                };





                listeVente.Add(ven);
            }

            vente = listeVente;
        }

        public static void ChargerListeAnnee()
        {
            //Création de la collection observable
            var listeA = new ObservableCollection<Vente>();
            //Appel de la fonction dans AccessDB pour la connection à la DB
            DataTable dt = AccessDB.Annee();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                var ven = new Vente

                {

                    //Création d'un nouvel objet de type province 
                   
                    AnneeDebut = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
                 
                };





                listeA.Add(ven);
            }

            ListeanneeDebut = listeA;
        }

        public static void ChargerListeAnneeFin()
        {
            //Création de la collection observable
            var listeF = new ObservableCollection<Vente>();
            //Appel de la fonction dans AccessDB pour la connection à la DB
            DataTable dt = AccessDB.Annee();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                var ven = new Vente

                {

                    //Création d'un nouvel objet de type province 

                    AnneeFin = Int32.Parse(dt.Rows[i]["Annee"].ToString()),

                };


                listeF.Add(ven);
            }

            ListeanneeFin = listeF;
        }

        /*  private string province;
          public string Province 
          {

              get
              {
                  return province;
              }
              set
              {
                  if (this.province != value)
                  {
                      this.province = value;
                      this.NotifyPropertyChanged("Province");
                  }
              }
          }*/
        /// <summary>
        /// je crée un attribut de type Province et je crée une propriété avec , qui est aussi de type Province
        /// </summary>
        private Province laProvince;

        public Province LaProvince
        {
            get
            {
                return laProvince;
            }

            set
            {
                if (this.laProvince != value)
                {
                    this.laProvince = value;
                    this.NotifyPropertyChanged("LaProvince");
                }
            }
        }
        /// <summary>
        /// je crée un attribut de type Vehicule et je crée une propriété avec, qui est aussi de type Vehicule
        /// </summary>
        private Vehicule lesGenres;
        public Vehicule LesGenres
        {
            get
            {
                return lesGenres;
            }

            set
            {
                if (this.lesGenres != value)
                {
                    this.lesGenres = value;
                    this.NotifyPropertyChanged("LesGenres");
                }
            }
        }

        private int anneeDebut;

        public int AnneeDebut
        {

            get
            {
                if (anneeFin<anneeDebut)
                {
                    anneeFin = anneeDebut;
                }
                return anneeDebut;
            }
            set
            {
                if (this.anneeDebut != value)
                {
                    this.anneeDebut = value;
                    this.NotifyPropertyChanged("AnneeDebut");
                }
            }
        }
        private int anneeFin;

        public int AnneeFin
        {
            get
            {

                if (anneeDebut > anneeFin)
                {
                    anneeDebut = anneeFin;
                }
                return anneeDebut;
            }
            set
            {
                if (this.anneeDebut != value)
                {
                    this.anneeDebut = value;
                    this.NotifyPropertyChanged("AnneeFin");
                }
            }
        }



        /// <summary>
        /// je crée un attribut de type string et je crée une propriété avec, qui est aussi de type string
        /// </summary>
        private string genre;
        public string Genre 
        {

            get
            {
                return genre;
            }
            set
            {
                if (this.genre != value)
                {
                    this.genre = value;
                    this.NotifyPropertyChanged("Genre");
                }
            }
        }

        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>
        private int annee;
        public int Annee 
        
        {

            get
            {
                return annee;
            }
            set
            {
                if (this.annee != value)
                {
                    this.annee = value;
                    this.NotifyPropertyChanged("Annee");
                }
            }


        }

        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>
        private int nbunites;
        public int NbUnites 
        {


            get
            {
                return nbunites;
            }
            set
            {
                if (this.nbunites != value)
                {
                    this.nbunites = value;
                    this.NotifyPropertyChanged("NbUnites");
                }
            }

        }
        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>

        private int montantx1000;
        public int Montantx1000 
        {
            get
            {
                return montantx1000;
            }
            set
            {
                if (this.montantx1000 != value)
                {
                    this.montantx1000 = value;
                    this.NotifyPropertyChanged("Montantx1000");
                }
            }
        }
        
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BLL;

namespace UIL
{
    /// <summary>
    /// Logique d'interaction pour UCSommeVentesProv.xaml
    /// </summary>
    /// 
    /// <summary>
    /// Nom:Daniel Kabeya
    /// Matricule:1812147
    /// </summary>
    public partial class UCSommeVentesProv : UserControl
    {
        List<int> listeAnnee = new List<int>();
        public UCSommeVentesProv()
        {
            InitializeComponent();

            Vente.ChargerListeAnnee();
            Vente.ChargerListeAnneeFin();
            Vehicule.ChargerListeVehicule();

            //cboAnneeDeb.ItemsSource = Vente.vente;
        }

        private void cboAnneeDeb_Loaded(object sender, RoutedEventArgs e)
        {
            
           
            cboAnneeDeb.ItemsSource = Vente.ListeanneeDebut;
           

            
    }

        private void cboAnneeDeb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            

           
        }

        private void cboGenre_Loaded(object sender, RoutedEventArgs e)
        {
            cboGenre.ItemsSource = Vehicule.vehicule;
        }

        private void cboAnneeFin_Loaded(object sender, RoutedEventArgs e)
        {
            cboAnneeFin.ItemsSource = Vente.ListeanneeFin;
        }
    }
}


0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
14 mai 2021 à 00:49
je viens de modifier mon code là mon combobox s'affiche mais quand je selectionne une année dans mon premier combobox la valeur du deuxième combox ne change càd que je peux toujours selectionner une année inferieure moi je ne veux pas ça
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
14 mai 2021 à 01:08
genre pour être plus explicite la valeur minimale de mon deuxième combobox doit être egale à la valeur selectionné dans mon premier combobox
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 928
14 mai 2021 à 09:34
Tu t'es compliqué la vie pour rien.

Voici un exemple autonome à adapter pour toi
        /// <summary>
        /// Pour l'exemple crée une liste d'annnées de 1900 à 2021
        /// </summary>
        private List<int> toutesLesannees = Enumerable.Range(1900, 122).ToList();

        private int anneeDebut = 0;
        /// <summary>
        /// Année de début à sélectionner
        /// </summary>
        public int AnneeDebut
        {
            get { return anneeDebut; }
            set
            {
                anneeDebut = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AnneeDebut"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AnneesFin"));//signale la réactualisation de la liste source du combo de fin

            }
        }

        private int anneeFin = 0;
        /// <summary>
        /// Année de fin à sélectionner
        /// </summary>
        public int AnneeFin
        {
            get { return anneeFin; }
            set
            {
                anneeFin = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AnneeFin"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AnneesDebut"));//signale la réactualisation de la liste source du combo de début
            }
        }

        /// <summary>
        /// Liste d'années pour le combo de début, qui dépend de la valeur de anneeFin
        /// </summary>
        public IEnumerable<int> AnneesDebut
        {
            get
            {
                return anneeFin == 0 ? toutesLesannees : toutesLesannees.Where(a => a < anneeFin);
            }
        }

        /// <summary>
        /// Liste d'années pour le combo de fin, qui dépend de la valeur de anneeDebut
        /// </summary>
        public IEnumerable<int> AnneesFin
        {
            get
            {
                return anneeDebut == 0 ? toutesLesannees : toutesLesannees.Where(a => a > anneeDebut);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;



        <ComboBox ItemsSource="{Binding AnneesDebut}" SelectedItem="{Binding AnneeDebut}" HorizontalAlignment="Left" Height="21" Margin="99,275,0,0" VerticalAlignment="Top" Width="130"/>
        <ComboBox ItemsSource="{Binding AnneesFin}" SelectedItem="{Binding AnneeFin}" HorizontalAlignment="Left" Height="21" Margin="264,275,0,0" VerticalAlignment="Top" Width="128"/>

0