IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Mise à jour du DataContext ? [MVVM]


Sujet :

Windows Presentation Foundation

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2016
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2016
    Messages : 61
    Points : 42
    Points
    42
    Par défaut Mise à jour du DataContext ?
    Salut les poneys !

    Voici donc un petit soucis, j'ai l'impression que mon DataContext ne se met pas à jour lors de la sélection d'un combobox qui doit alimenter en alimenter un autre !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <ComboBox Width="150" ItemsSource="{Binding Clients}" SelectedIndex="-1" SelectedItem="{Binding SelectedClient}" />
    <ComboBox Width="150" ItemsSource="{Binding Databases}" SelectedIndex="-1" />
    Le code C# associé est:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
    class ClientVM
    {
    ClientsDataContext dc;
            private List<string> _clients;
            public List<string> Clients
            {
                get { return _clients; }
                set { _clients = value; OnPropertyChanged("Clients"); }
            }
     
            private List<string> _databases;
            public List<string> Databases
            {
                get { return _databases; }
                set { _databases = value; OnPropertyChanged("Databases"); }
            }
     
            private string _selectedClient;
            public string SelectedClient
            {
                get { return _selectedClient; }
                set { _selectedClient = value; GetClientDatabases(); }
            }
    public List<string> GetClients()
            {
                _clients = (from c in dc.Clients orderby c.Name ascending select c.Name).ToList();
                return _clients;
            }
     public void GetClientDatabases()
            {
                var clientID = (from c in dc.Clients where c.Name == _selectedClient select c.Id).First();
                _databases = (from d in dc.Databases orderby d.Name ascending where d.Client_ID == clientID select d.Name).ToList();
            }
    A noter qu'en dehors que ma combobox "Databases" ne se met pas à jour, la propriété "Databases/_databases" se met bel et bien à jour
    Voili-voilou, je suis preneur de toute bonne idée :p

    Dans l'attente de vos nouvelles, Bonne journée
    Cordialement,

    yagaam

  2. #2
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    C'est parceque ta source Databases est une List<T>, qui ne procure aucune information de changement (INotifyCollectionChanged).
    Utilise à la place une ObservableCollection<T>, et au lieux d'en affecter une nouvelle, tu fais un clear et tu ajoutes les elements issues de ta nouvelle liste.
    Utilise cette extension:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class RangeEnabledObservableCollection<T> : ObservableCollection<T>
    {
        public void InsertRange(IEnumerable<T> items) 
        {
            this.CheckReentrancy();
            foreach(var item in items)
                this.Items.Add(item);
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }
    si tu ne veux pas generer l'evenement de changement de collection à chaque Add (en meme temps si tu as des petites collections c'est pas génant).

  3. #3
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2016
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2016
    Messages : 61
    Points : 42
    Points
    42
    Par défaut
    Merci à toi ! ; mon code actuel du coup est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    private ObservableCollection<string> _clients;
            public ObservableCollection<string> Clients
            {
                get { return _clients; }
                set { _clients = value; OnPropertyChanged("Clients"); OnPropertyChanged("Databases"); }
            }
     
            private RangeEnabledObservableCollection<string> _databases;
            public RangeEnabledObservableCollection<string> Databases
            {
                get { return _databases; }
                set { _databases = value; OnPropertyChanged("Databases"); }
            }
     
            private string _selectedClient;
            public string SelectedClient
            {
                get { return _selectedClient; }
                set { _selectedClient = value; GetClientDatabases(); }
            }
     
     public void GetClients()
            {
                var query = from c in dc.Clients orderby c.Name ascending select c.Name;
                _clients = new ObservableCollection<string>(query);
            }
     
     public void GetClientDatabases()
            {
                var clientID = (from c in dc.Clients where c.Name == _selectedClient select c.Id).First();
                var query = from d in dc.Databases orderby d.Name ascending where d.Client_ID == clientID select d.Name;
                _databases = new RangeEnabledObservableCollection<string>();
                _databases.Clear();
     
     
                foreach (var data in query)
                    _databases.Add(data);
            }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public partial class DBExplorerView : UserControl
        {
            public DBExplorerView()
            {
                InitializeComponent();
                DataContext = new ClientVM();
            }
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <ComboBox Width="150" ItemsSource="{Binding Clients}" SelectedItem="{Binding SelectedClient, UpdateSourceTrigger=PropertyChanged}" />
    <ComboBox Width="150" ItemsSource="{Binding Databases}" />
    Fonctionne pas ; même résultat
    Cordialement,

    yagaam

  4. #4
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    Oublie l'observableCollection tu n'en as pas besoin dans ton cas de figure (et en plus utilises incorrectement l'extension )).
    En reprenant ton 1er code il faut juste passer par la proprété Databases et non le champs _databases, de ce fait tu passeras automatiquement par le Set (et donc le OnPropertyChanged("Databases")

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    class ClientVM
    {
    ClientsDataContext dc;
            private List<string> _clients;
            public List<string> Clients
            {
                get { return _clients; }
                set { _clients = value; OnPropertyChanged("Clients"); }
            }
    
            private List<string> _databases;
            public List<string> Databases
            {
                get { return _databases; }
                set { _databases = value; OnPropertyChanged("Databases"); }
            }
    
            private string _selectedClient;
            public string SelectedClient
            {
                get { return _selectedClient; }
                set { _selectedClient = value; GetClientDatabases(); }
            }
    public List<string> GetClients()
            {
                _clients = (from c in dc.Clients orderby c.Name ascending select c.Name).ToList();
                return _clients;
            }
     public void GetClientDatabases()
            {
                var clientID = (from c in dc.Clients where c.Name == _selectedClient select c.Id).First();
               Databases = (from d in dc.Databases orderby d.Name ascending where d.Client_ID == clientID select d.Name).ToList();
            }
    De manière génerale il vaut toujours mieux passer par la propriété que par le champ, justement pour bénéficier du Get/Set.

  5. #5
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2016
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2016
    Messages : 61
    Points : 42
    Points
    42
    Par défaut
    Hello,

    Le combobox continu de ne pas se remplir ^^ Mais Databases récupère bien les bonnes informations

    bizare non ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
     class ClientVM : RangeEnabledObservableCollection<ClientModel>
        {
            #region properties
            ClientsDataContext dc;
            private List<string> _clients;
            public List<string> Clients
            {
                get { return _clients; }
                set { _clients = value; OnPropertyChanged("Clients"); }
            }
     
            private List<string> _databases;
            public List<string> Databases
            {
                get { return _databases; }
                set { _databases = value; OnPropertyChanged("Databases"); }
            }
     
            private string _selectedClient;
            public string SelectedClient
            {
                get { return _selectedClient; }
                set { _selectedClient = value; GetClientDatabases(); }
            }
     
     
            private RelayCommand _openClientFormCommand;
            public RelayCommand OpenClientFormCommand
            {
                get
                {
                    if (_openClientFormCommand == null)
                        _openClientFormCommand = new RelayCommand(OpenClientForm);
                    return _openClientFormCommand;
                }
            }
     
            #endregion
     
            #region constructor
            public ClientVM()
            {
                dc = new ClientsDataContext();
                GetClients();
            }
            #endregion
     
            #region methods
     
            public List<string> GetClients()
            {
                _clients = (from c in dc.Clients orderby c.Name ascending select c.Name).ToList();
                return _clients;
            }
            public void GetClientDatabases()
            {
                var clientID = (from c in dc.Clients where c.Name == _selectedClient select c.Id).First();
                Databases = (from d in dc.Databases orderby d.Name ascending where d.Client_ID == clientID select d.Name).ToList();
            }
     
            public void OpenClientForm(object o)
            {
                ClientFormView clientView = new ClientFormView();
                clientView.Show();
            }
            #endregion
     
            #region PropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     class RangeEnabledObservableCollection<T> : ObservableCollection<T>
        {
            public void InsertRange(IEnumerable<T> items)
            {
                CheckReentrancy();
                foreach (var item in items)
                    Items.Add(item);
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <ComboBox Width="150" ItemsSource="{Binding Clients}" SelectedItem="{Binding SelectedClient, UpdateSourceTrigger=PropertyChanged}" />
    <ComboBox Width="150" ItemsSource="{Binding Databases}" />
    Cordialement,

    yagaam

  6. #6
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    Mise à part que tu devrais hériter simplement de INotifyPropertyChanged et pas de RangeEnabledObservableCollection, je vois rien d'etrange

  7. #7
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2016
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2016
    Messages : 61
    Points : 42
    Points
    42
    Par défaut
    Et TADAAAAM,

    Merci à toi

    Bonne journée !
    Cordialement,

    yagaam

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 11
    Dernier message: 11/11/2009, 17h58
  2. Mise à jour du DataContext
    Par Gregory.M dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 06/11/2008, 15h40
  3. Mise à jour de la version de MySQL
    Par jobstar dans le forum Administration
    Réponses: 8
    Dernier message: 18/08/2003, 10h45
  4. mise à jour de champs time (interbase)
    Par pram dans le forum XMLRAD
    Réponses: 6
    Dernier message: 04/03/2003, 10h25
  5. Réponses: 2
    Dernier message: 12/02/2003, 15h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo