2009-10-20 6 views
1

J'essaye de synchroniser les barres de défilement verticales de deux grilles de propriété. L'idée est quand un utilisateur fait défiler une grille de propriété l'autre grille de propriété défile de la même quantité.Comment est-ce que je peux attraper des événements de défilement dans des formes de Windows PropertyGrid

Ma première approche consistait à gérer l'événement de défilement, mais il semble que PropertyGrid ne génère pas ce type d'événement. J'ai regardé dans les contrôles contenus dans le PropertyGrid et il y a un PropertyGridView, que je parie que c'est le contrôle avec la barre de défilement.

Est-ce que quelqu'un sait une solution de contournement pour réaliser ce que je veux?

Merci.

Répondre

5

Celui-ci montre la synchronisation avec le PropertyGridView voisin. Notez que vous devrez l'étendre pour gérer l'utilisateur en cliquant sur l'un ou l'autre contrôle. Cette version met à jour propertyGrid2 pour correspondre à propertyGrid1, mais pas vice versa.

using System; 
using System.Windows.Forms; 
using System.Reflection; 

namespace WindowsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     Control m_pgv_1 = null; 
     Control m_pgv_2 = null; 
     MethodInfo m_method_info; 

     public Form1() 
     { 
      InitializeComponent(); 

      // Set the Property Grid Object to something 
      propertyGrid1.SelectedObject = dataGridView1; 
      propertyGrid2.SelectedObject = dataGridView1; 

      // Loop through sub-controlls and find PropertyGridView 
      m_pgv_1 = FindControl (propertyGrid1.Controls, "PropertyGridView"); 
      m_pgv_2 = FindControl (propertyGrid2.Controls, "PropertyGridView"); 

      // Reflection trickery to get a private/internal field 
      // and method, scrollBar and SetScrollOffset in this case 
      Type type = m_pgv_1.GetType(); 
      FieldInfo f = FindField (type, "scrollBar"); 
      m_method_info = FindMethod (type, "SetScrollOffset"); 

      // Get the scrollBar for our PropertyGrid and add the event handler 
      ((ScrollBar)f.GetValue (m_pgv_1)).Scroll += 
       new ScrollEventHandler (propertyGrid1_Scroll); 
     } 

     private void propertyGrid1_Scroll (object sender, ScrollEventArgs e) 
     { 
      System.Console.WriteLine ("Scroll"); 

      // Set the new scroll position on the neighboring 
      // PropertyGridView 
      object [] parameters = { e.NewValue }; 
      m_method_info.Invoke (m_pgv_2, parameters); 
     } 

     private static Control FindControl (
      Control.ControlCollection controls, string name) 
     { 
      foreach (Control c in controls) 
      { 
       if (c.Text == name) 
        return c; 
      } 

      return null; 
     } 

     private static MethodInfo FindMethod (Type type, string method) 
     { 
      foreach (MethodInfo mi in type.GetMethods()) 
      { 
       if (method == mi.Name) 
        return mi; 
      } 

      return null; 
     } 

     private static FieldInfo FindField (Type type, string field) 
     { 
      FieldInfo f = type.GetField (field, 
       BindingFlags.Instance | BindingFlags.NonPublic); 

      return f; 
     } 
    } 
} 
0

Il faut un peu de ruse, mais cela devrait le faire:

using System; 
using System.Windows.Forms; 
using System.Reflection; 

namespace WindowsApplication1 { 
    public partial class Form1 : Form { 
     Control m_pgv = null; 

     public Form1() { 
      InitializeComponent(); 

      // Set the Property Grid Object to something 
      propertyGrid1.SelectedObject = dataGridView1; 

      // Loop through sub-controls and find PropertyGridView 
      foreach (Control c in propertyGrid1.Controls) { 
       if (c.Text == "PropertyGridView") 
       { 
        m_pgv = (Control)c; 
        break; 
       } 
      } 

      // Reflection trickery to get a private field, 
      // scrollBar in this case 
      Type t = m_pgv.GetType(); 
      FieldInfo f = t.GetField("scrollBar", 
       BindingFlags.Instance | BindingFlags.NonPublic); 

      // Get the scrollBar for our PropertyGrid and add the event handler 
      ScrollBar sb = (ScrollBar) f.GetValue(m_pgv); 
      sb.Scroll += new ScrollEventHandler(propertyGrid1_Scroll); 
     } 

     private void propertyGrid1_Scroll (object sender, ScrollEventArgs e) { 
      System.Console.WriteLine ("Scroll"); 
     } 
    } 
} 
+0

Comment obtenez-vous un autre contrôle pour faire défiler le premier? –

+0

En utilisant ce code, j'ai été capable de synchroniser les deux barres de défilement. Quand je bouge l'un l'autre bouge en conséquence. Le problème est que le contenu ne défile pas avec la barre de défilement. Je suppose qu'il est préférable d'abandonner mon idée originale. – jassuncao

+0

Vous devez appeler SetScrollPosition() sur le PropertyGridView voisin. Voir ma deuxième réponse. –

Questions connexes