private void _ButtonAffaireCommandeNouveau_Click(object sender, RoutedEventArgs e)
        {
            CommandeWindow commandeWindow = new CommandeWindow();
            commandeWindow.DataContext = new Commande_Fournisseur();
            ((Commande_Fournisseur)commandeWindow.DataContext).Affaire1 = (Affaire)this.DataContext;
            commandeWindow._comboBoxAffaire.IsEnabled = false;

            bool? dialogResult = commandeWindow.ShowDialog();

            if (dialogResult.HasValue && dialogResult.Value == true)
            {
                ((Affaire)this.DataContext).Commande_Fournisseur.Add((Commande_Fournisseur)commandeWindow.DataContext);
            }
            else
            {
                try
                {
                    ((App)App.Current).mySitaffEntities.Detach((Commande_Fournisseur)commandeWindow.DataContext);
                }
                catch (Exception)
                {

                }
            }
            this._dataGridCommande.Items.Refresh();
        }
        private void _ButtonAffaireCommandeModifier_Click(object sender, RoutedEventArgs e)
        {
            if (this._dataGridCommande.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Vous devez sélectionner une commande à modifier.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (this._dataGridCommande.SelectedItems.Count > 1)
            {
                MessageBox.Show("Vous ne devez sélectionner qu'une commande à modifier.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (this._dataGridCommande.SelectedItem != null)
            {
                CommandeWindow commandeWindow = new CommandeWindow();
                commandeWindow.DataContext = (Commande_Fournisseur)this._dataGridCommande.SelectedItem;

                bool? dialogResult = commandeWindow.ShowDialog();

                if (dialogResult.HasValue && dialogResult.Value == true)
                {
                    this._dataGridCommande.SelectedItem = (Commande_Fournisseur)commandeWindow.DataContext;
                }
                else
                {
                    try
                    {
                        ((App)App.Current).mySitaffEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, (Commande_Fournisseur)commandeWindow.DataContext);
                    }
                    catch (Exception)
                    {
                    }

                }
            }
            this._dataGridCommande.Items.Refresh();
        }
        /// <summary>
        /// Ajoute une nouvelle Commande_Fournisseur à la liste à l'aide d'une nouvelle fenêtre
        /// </summary>
        public Commande_Fournisseur Add()
        {
            //Affichage du message "ajout en cours"
            ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = true;
            ((App)App.Current)._theMainWindow.changementTexteStatusBar("Ajout d'une commande fournisseur en cours ...");

            //Initialisation de la fenêtre
            ChoixTypeCommandeWindow choixTypeCommandeWindows = new ChoixTypeCommandeWindow();
            CommandeWindow commandeWindow = new CommandeWindow();

            //Création de l'objet temporaire
            Commande_Fournisseur tmp = new Commande_Fournisseur();
            tmp.Utilisateur1 = ((App)App.Current)._connectedUser;

            //Mise de l'objet temporaire dans le datacontext
            commandeWindow.DataContext = tmp;

            //Définition du type de commande
            bool? dialogChoix = choixTypeCommandeWindows.ShowDialog();

            if (dialogChoix.HasValue && dialogChoix.Value == true)
            {
                if (choixTypeCommandeWindows.affaire != null)
                {
                    ((Commande_Fournisseur)commandeWindow.DataContext).Affaire1 = choixTypeCommandeWindows.affaire;
                }
                if (choixTypeCommandeWindows.stock == true)
                {
                    ((Commande_Fournisseur)commandeWindow.DataContext).Stock = true;
                }
                if (choixTypeCommandeWindows.divers == true)
                {
                    ((Commande_Fournisseur)commandeWindow.DataContext).Divers = true;
                }
                ((Commande_Fournisseur)commandeWindow.DataContext).Entreprise_Mere1 = choixTypeCommandeWindows.entreprise_mere;

                //booléen nullable vrai ou faux ou null
                bool? dialogResult = commandeWindow.ShowDialog();

                if (dialogResult.HasValue && dialogResult.Value == true)
                {
                    //Si j'appuie sur le bouton Ok, je renvoi l'objet commande se trouvant dans le datacontext de la fenêtre
                    return (Commande_Fournisseur)commandeWindow.DataContext;
                }
                else
                {
                    try
                    {
                        //On détache tous les élements liés à la commande Commande_Fournisseur_Condition_Reglement
                        ObservableCollection<Commande_Fournisseur_Condition_Reglement> toRemove = new ObservableCollection<Commande_Fournisseur_Condition_Reglement>();
                        foreach (Commande_Fournisseur_Condition_Reglement item in ((Commande_Fournisseur)commandeWindow.DataContext).Commande_Fournisseur_Condition_Reglement)
                        {
                            toRemove.Add(item);
                        }
                        foreach (Commande_Fournisseur_Condition_Reglement item in toRemove)
                        {
                            ((App)App.Current).mySitaffEntities.Detach(item);
                        }

                        //On détache tous les élements liés à la commande Contenu_Commande_Fournisseur
                        ObservableCollection<Contenu_Commande_Fournisseur> toRemove1 = new ObservableCollection<Contenu_Commande_Fournisseur>();
                        foreach (Contenu_Commande_Fournisseur item in ((Commande_Fournisseur)commandeWindow.DataContext).Contenu_Commande_Fournisseur)
                        {
                            toRemove1.Add(item);
                        }
                        foreach (Contenu_Commande_Fournisseur item in toRemove1)
                        {
                            ((App)App.Current).mySitaffEntities.Detach(item);
                        }

                        //On détache tous les élements liés à la commande Bon_Livraison
                        ObservableCollection<Bon_Livraison> toRemove2 = new ObservableCollection<Bon_Livraison>();
                        foreach (Bon_Livraison item in ((Commande_Fournisseur)commandeWindow.DataContext).Bon_Livraison)
                        {
                            toRemove2.Add(item);
                        }
                        foreach (Bon_Livraison item in toRemove2)
                        {
                            ((App)App.Current).mySitaffEntities.Detach(item);
                        }

                        //On détache tous les élements liés à la commande Facture_Proforma
                        ObservableCollection<Facture_Proforma> toRemove3 = new ObservableCollection<Facture_Proforma>();
                        foreach (Facture_Proforma item in ((Commande_Fournisseur)commandeWindow.DataContext).Facture_Proforma)
                        {
                            toRemove3.Add(item);
                        }
                        foreach (Facture_Proforma item in toRemove3)
                        {
                            ((App)App.Current).mySitaffEntities.Detach(item);
                        }

                        //On détache la commande
                        ((App)App.Current).mySitaffEntities.Detach((Commande_Fournisseur)commandeWindow.DataContext);
                    }
                    catch (Exception)
                    {
                    }

                    //Si j'appuie sur le bouton annuler, je préviens que j'annule mon ajout
                    ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = false;
                    this.recalculMax();
                    ((App)App.Current)._theMainWindow.changementTexteStatusBar("Ajout d'une commande fournisseur annulé : " + this.listCommandes.Count() + " / " + this.max);

                    return null;
                }
            }
            else
            {
                try
                {
                    ((App)App.Current).mySitaffEntities.Detach((Commande_Fournisseur)commandeWindow.DataContext);
                    //Pas besoin de détacher le reste car ici l'utilisateur annule avant qu'il n'ait pu insérer quoi que ce soit
                }
                catch (Exception)
                {
                }

                //Si j'appuie sur le bouton annuler, je préviens que j'annule mon ajout
                ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = false;
                this.recalculMax();
                ((App)App.Current)._theMainWindow.changementTexteStatusBar("Ajout d'une commande fournisseur annulé : " + this.listCommandes.Count() + " / " + this.max);

                return null;
            }
        }
        /// <summary>
        /// Ouvre la commande fournisseur séléctionnée à l'aide d'une nouvelle fenêtre
        /// </summary>
        public Commande_Fournisseur Open()
        {
            if (this._DataGridMain.SelectedItem != null)
            {
                if (this._DataGridMain.SelectedItems.Count == 1)
                {
                    //Affichage du message "modification en cours"
                    ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = true;
                    ((App)App.Current)._theMainWindow.changementTexteStatusBar("Modification d'une commande fournisseur en cours ...");

                    //Création de la fenêtre
                    CommandeWindow commandeWindow = new CommandeWindow();

                    //Initialisation du Datacontext en Commande_Fournisseur et association à la Commande_Fournisseur sélectionnée
                    commandeWindow.DataContext = new Commande_Fournisseur();
                    commandeWindow.DataContext = (Commande_Fournisseur)this._DataGridMain.SelectedItem;
                    ((Commande_Fournisseur)commandeWindow.DataContext).Utilisateur1 = ((App)App.Current)._connectedUser;

                    //booléen nullable vrai ou faux ou null
                    bool? dialogResult = commandeWindow.ShowDialog();

                    if (dialogResult.HasValue && dialogResult.Value == true)
                    {
                        //Si j'appuie sur le bouton Ok, je renvoi l'objet DAO se trouvant dans le datacontext de la fenêtre
                        return (Commande_Fournisseur)commandeWindow.DataContext;
                    }
                    else
                    {
                        //Je récupère les anciennes données de la base sur les modifications effectuées
                        ((App)App.Current).mySitaffEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, (Commande_Fournisseur)(this._DataGridMain.SelectedItem));
                        //La commande étant un objet "critique" au niveau des associations, je refresh l'edmx et je relance le filtrage s'il y en avait un afin d'avoir les mêmes infos (invisible pour l'user)
                        ((App)App.Current).refreshEDMXSansVidage();
                        this.filtrage();

                        //Si j'appuie sur le bouton annuler, je préviens que j'annule ma modification
                        ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = false;
                        this.recalculMax();
                        ((App)App.Current)._theMainWindow.changementTexteStatusBar("Modification d'une commande fournisseur annulée : " + this.listCommandes.Count() + " / " + this.max);

                        return null;
                    }
                }
                else
                {
                    MessageBox.Show("Vous ne devez sélectionner qu'une seule commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return null;
                }
            }
            else
            {
                MessageBox.Show("Vous devez sélectionner une commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return null;
            }
        }
        /// <summary>
        /// Ouvre la commande fournisseur séléctionnée en lecture seule à l'aide d'une nouvelle fenêtre
        /// </summary>
        public Commande_Fournisseur Look(Commande_Fournisseur commande_fournisseur)
        {
            if (this._DataGridMain.SelectedItem != null || commande_fournisseur != null)
            {
                if (this._DataGridMain.SelectedItems.Count == 1 || commande_fournisseur != null)
                {
                    //Affichage du message "affichage en cours"
                    ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = true;
                    ((App)App.Current)._theMainWindow.changementTexteStatusBar("Affichage d'une commande fournisseur en cours ...");

                    //Création de la fenêtre
                    CommandeWindow commandeWindow = new CommandeWindow();

                    //Initialisation du Datacontext en Commande_Fournisseur et association à la Commande_Fournisseur sélectionnée
                    commandeWindow.DataContext = new Commande_Fournisseur();
                    if (commande_fournisseur != null)
                    {
                        commandeWindow.DataContext = commande_fournisseur;
                    }
                    else
                    {
                        commandeWindow.DataContext = (Commande_Fournisseur)this._DataGridMain.SelectedItem;
                    }

                    //Je positionne la lecture seule sur la fenêtre
                    commandeWindow.lectureSeule();
                    commandeWindow.soloLecture = true;

                    //J'affiche la fenêtre
                    bool? dialogResult = commandeWindow.ShowDialog();

                    //Affichage du message "affichage en cours"
                    ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = false;
                    ((App)App.Current)._theMainWindow.changementTexteStatusBar("Affichage d'une commande fournisseur terminé : " + this.listCommandes.Count() + " / " + this.max);

                    //Renvoi null
                    return null;
                }
                else
                {
                    MessageBox.Show("Vous ne devez sélectionner qu'une seule commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return null;
                }
            }
            else
            {
                MessageBox.Show("Vous devez sélectionner une commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return null;
            }
        }
        /// <summary>
        /// duplique une Commande_Fournisseur à la liste à l'aide d'une nouvelle fenêtre
        /// </summary>
        public Commande_Fournisseur Duplicate()
        {
            if (this._DataGridMain.SelectedItem != null)
            {
                if (this._DataGridMain.SelectedItems.Count == 1)
                {
                    //Affichage du message "ajout en cours"
                    ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = true;
                    ((App)App.Current)._theMainWindow.changementTexteStatusBar("Dupliquer une commande fournisseur en cours ...");

                    //Création de la fenêtre
                    CommandeWindow commandeWindow = new CommandeWindow();

                    //Duplication de la commande sélectionnée
                    Commande_Fournisseur tmp = new Commande_Fournisseur();
                    tmp = duplicateCommande((Commande_Fournisseur)this._DataGridMain.SelectedItem);

                    //Activation de la comboBox Affaire si la commande sélectionnée était sur une affaire
                    if (((Commande_Fournisseur)this._DataGridMain.SelectedItem).Affaire1 != null)
                    {
                        commandeWindow._comboBoxAffaire.IsEnabled = true;
                    }

                    //Association de l'élement dupliqué au datacontext de la fenêtre
                    commandeWindow.DataContext = tmp;

                    //booléen nullable vrai ou faux ou null
                    bool? dialogResult = commandeWindow.ShowDialog();

                    if (dialogResult.HasValue && dialogResult.Value == true)
                    {
                        return (Commande_Fournisseur)commandeWindow.DataContext;
                    }
                    else
                    {
                        try
                        {
                            //On détache tous les élements liés à la commande Commande_Fournisseur_Condition_Reglement
                            ObservableCollection<Commande_Fournisseur_Condition_Reglement> toRemove = new ObservableCollection<Commande_Fournisseur_Condition_Reglement>();
                            foreach (Commande_Fournisseur_Condition_Reglement item in ((Commande_Fournisseur)commandeWindow.DataContext).Commande_Fournisseur_Condition_Reglement)
                            {
                                toRemove.Add(item);
                            }
                            foreach (Commande_Fournisseur_Condition_Reglement item in toRemove)
                            {
                                ((App)App.Current).mySitaffEntities.Detach(item);
                            }

                            //On détache tous les élements liés à la commande Contenu_Commande_Fournisseur
                            ObservableCollection<Contenu_Commande_Fournisseur> toRemove1 = new ObservableCollection<Contenu_Commande_Fournisseur>();
                            foreach (Contenu_Commande_Fournisseur item in ((Commande_Fournisseur)commandeWindow.DataContext).Contenu_Commande_Fournisseur)
                            {
                                toRemove1.Add(item);
                            }
                            foreach (Contenu_Commande_Fournisseur item in toRemove1)
                            {
                                ((App)App.Current).mySitaffEntities.Detach(item);
                            }

                            //On détache tous les élements liés à la commande Bon_Livraison
                            ObservableCollection<Bon_Livraison> toRemove2 = new ObservableCollection<Bon_Livraison>();
                            foreach (Bon_Livraison item in ((Commande_Fournisseur)commandeWindow.DataContext).Bon_Livraison)
                            {
                                toRemove2.Add(item);
                            }
                            foreach (Bon_Livraison item in toRemove2)
                            {
                                ((App)App.Current).mySitaffEntities.Detach(item);
                            }

                            //On détache tous les élements liés à la commande Facture_Proforma
                            ObservableCollection<Facture_Proforma> toRemove3 = new ObservableCollection<Facture_Proforma>();
                            foreach (Facture_Proforma item in ((Commande_Fournisseur)commandeWindow.DataContext).Facture_Proforma)
                            {
                                toRemove3.Add(item);
                            }
                            foreach (Facture_Proforma item in toRemove3)
                            {
                                ((App)App.Current).mySitaffEntities.Detach(item);
                            }

                            //On détache la commande
                            ((App)App.Current).mySitaffEntities.Detach((Commande_Fournisseur)commandeWindow.DataContext);
                        }
                        catch (Exception)
                        {
                        }

                        //Si j'appuie sur le bouton annuler, je préviens que j'annule mon ajout
                        ((App)App.Current)._theMainWindow.progressBarMainWindow.IsIndeterminate = false;
                        this.recalculMax();
                        ((App)App.Current)._theMainWindow.changementTexteStatusBar("Dupliquer une commande fournisseur annulé : " + this.listCommandes.Count() + " / " + this.max);

                        return null;
                    }
                }
                else
                {
                    MessageBox.Show("Vous ne devez sélectionner qu'une seule commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return null;
                }
            }
            else
            {
                MessageBox.Show("Vous devez sélectionner une commande fournisseur.", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return null;
            }
        }