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 Forms Discussion :

PropertyGrid.PropertyValueChanged et objets imbriqués


Sujet :

Windows Forms

  1. #1
    Membre averti
    Homme Profil pro
    Développeur de jeux vidéo
    Inscrit en
    Octobre 2008
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur de jeux vidéo

    Informations forums :
    Inscription : Octobre 2008
    Messages : 187
    Points : 448
    Points
    448
    Par défaut PropertyGrid.PropertyValueChanged et objets imbriqués
    Bonjour tout le monde,

    Est-il possible de connaître l'objet qui est modifié dans la callback PropertyValueChanged d'une PropertyGrid ? Je m'explique : dans le but d'implémenter un Ctrl-Z dans mon application, je souscrit à l'évenement PropertyValueChanged pour stocker les paramètres qui ont changés :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class MyPropertyGrid : PropertyGrid
    {
        public MyPropertyGrid()
        {
            InitializeComponent();
            this.PropertyValueChanged += PropertyChanged;
        }
     
        private void PropertyChanged( object sender, PropertyValueChangedEventArgs args )
        {
            commandManager.Do( new PropertyCommand( this.SelectedObject, args.ChangedItem.PropertyDescriptor, args.ChangedItem.Value, args.OldValue );
        }
     
    }
    et dans ma classe PropertyCommand :

    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
    public class PropertyCommand : ICommand
    {
        public PropertyCommand( object self, PropertyDescriptor property, object newValue, object oldValue )
        {
            self_ = self;
            property_ = property;
            newValue_ = newValue;
            oldValue_ = oldValue;
        }
     
        public void Do()
        {
            property.SetValue( self_, newValue_ );
        }
     
        public void Undo()
        {
            property_.SetValue( self_, oldValue_ );
        }
     
        private readonly object self_;
        private readonly PropertyDescriptor property_;
        private readonly object newValue_;
        private readonly object oldValue_;
     
    }
    Le problème, c'est que le champ this.SelectedObiect ne correspond pas toujours à l'objet qui vient d'être modifé : ce n'est pas le cas par exemple si l'on a des objets imbriqués dans la propertygrid, et du coup, les méthodes Do() et Undo() provoquent des exception...

    J'ai beau chercher dans la classe PropertyGrid ou dans GridItem (et dans la doc MSDN), je ne trouve pas ce fameux objet modifié. Toutte aide serait la bienvenue, merci d'avance.

  2. #2
    Membre averti
    Homme Profil pro
    Développeur de jeux vidéo
    Inscrit en
    Octobre 2008
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur de jeux vidéo

    Informations forums :
    Inscription : Octobre 2008
    Messages : 187
    Points : 448
    Points
    448
    Par défaut
    Bon j'ai trouvé comment contourner le problème : je vais utiliser la sérialisation pour sauvegarder récursivement l'état de mon objet avant et après le PropertyChanged.

    Pour info, j'obtiens :

    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
    public class MyPropertyGrid : PropertyGrid
    {
        public MyPropertyGrid()
        {
            InitializeComponent();
            this.PropertyValueChanged += PropertyChanged;
     
            serialized_ = null;
        }
     
        public void SetSelectedObject( object selectedObject )
        {
            this.SelectedObject = selectedObject;
            serialized = ( selectedObject == null ) ? null : Serialize( selectedObject );
        }
     
        private void PropertyChanged( object sender, PropertyValueChangedEventArgs args )
        {
            commandManager.Do( new SerializeCommand( this.SelectedObject, Serialize( this.SelectedObject ), serialized_ );
        }
     
        private string serialized_;
     
    }
    dans ma classe SerializeCommand:

    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
    public class SerializeCommand: ICommand
    {
        public SerializeCommand( object self, string newValue, string oldValue )
        {
            self_ = self;
            newValue_ = newValue;
            oldValue_ = oldValue;
        }
     
        public void Do()
        {
            self_ = Deserialize( newValue_, self_ );
        }
     
        public void Undo()
        {
             self_ = Deserialize( oldValue_, self_ );
        }
     
        private readonly object self_;
        private readonly string newValue_;
        private readonly string oldValue_;
     
    }

Discussions similaires

  1. [MVC3.2/JQuery] $.ajax() ou $.post() avec objets imbriqués
    Par CUCARACHA dans le forum ASP.NET MVC
    Réponses: 4
    Dernier message: 31/05/2012, 11h32
  2. DataBinding d'objets imbriqués
    Par smarties dans le forum Silverlight
    Réponses: 12
    Dernier message: 20/04/2011, 16h24
  3. Objet imbriqués problème compilation
    Par ludo86 dans le forum Débuter
    Réponses: 2
    Dernier message: 30/07/2009, 00h29
  4. Créer des objets imbriqués
    Par Ludix dans le forum Macros et VBA Excel
    Réponses: 20
    Dernier message: 30/05/2008, 15h53

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