2009-10-20 93 views
1

我試圖同步兩個屬性網格的垂直滾動條。這個想法是當一個用戶滾動一個屬性網格時,其他屬性網格滾動相同的數量。如何在Windows窗體中捕獲滾動事件PropertyGrid

我的第一種方法是處理滾動事件,但似乎PropertyGrid不會產生這種事件。我查看了PropertyGrid中包含的控件,並且有一個PropertyGridView,我敢打賭是用滾動條控制的。

有沒有人知道解決方法來實現我想要的?

謝謝。

回答

5

這其中顯示了與鄰國PropertyGridView同步。請注意,您必須將其擴展以處理用戶單擊任一控件。此版本更新propertyGrid2以匹配propertyGrid1,但不是相反。

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

它需要一點點欺騙,但是這應該這樣做:

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

那麼您如何獲得另一個控件滾動條以匹配第一個? – 2009-10-21 00:09:13

+0

使用此代碼我能夠同步兩個滾動條。當我移動另一個相應的移動。問題在於內容不會隨滾動條一起滾動。我想最好放棄我最初的想法。 – jassuncao 2009-10-21 09:31:58

+0

您必須在相鄰的PropertyGridView上調用SetScrollPosition()。看到我的第二個答案。 – 2009-10-21 14:50:53