2012-12-06 27 views
7

मैं View.csDataGridColumnHeader ContextMenu प्रोग्राम के रूप में

var contextMenu = this.dataGridFacade.GiveContextMenuForDataGrid(this.DataGridAllJobs); 

this.DataGridAllJobs.ContextMenu = contextMenu; 

में इस कोड है लेकिन मैं केवल शीर्ष लेख के लिए इस संदर्भ मेनू जोड़ना चाहते हैं। क्या यह संभव है?

उत्तर

7

आपको बस अपने डेटाग्रिड के DataGridColumnHeadersPresenter को पुनर्प्राप्त करना होगा और इसके कॉन्टेक्स्टमेनू को सेट करना होगा।

public static class Visual_ExtensionMethods 
{ 
    /// <summary> 
    /// Retrieves the first Descendant of the currren Visual in the VisualTree matching the given predicate 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="this">The current Visual.</param> 
    /// <param name="predicate">An optional predicate that the descendant have to satisfy.</param> 
    /// <returns></returns> 
    public static T SafeFindDescendant<T>(this Visual @this, Predicate<T> predicate = null) where T : Visual 
    { 
     T result = null; 
     if (@this == null) 
     { 
      return null; 
     } 

     // iterate on VisualTree children thanks to VisualTreeHelper 
     int childrenCount = VisualTreeHelper.GetChildrenCount(@this); 
     for (int i = 0; i < childrenCount; i++) 
     { 
      var currentChild = VisualTreeHelper.GetChild(@this, i); 

      var typedChild = currentChild as T; 
      if (typedChild == null) 
      { 
       // recursive search 
       result = ((Visual)currentChild).SafeFindDescendant<T>(predicate); 
       if (result != null) 
       { 
        break; 
       } 
      } 
      else 
      { 
       if (predicate == null || predicate(typedChild)) 
       { 
        result = typedChild; 
        break; 
       } 
      } 
     } 

     return result; 
    } 
} 
+0

मैं आप इस 50 प्रतिष्ठा अंक जीतने thnink:

var contextMenu = this.dataGridFacade.GiveContextMenuForDataGrid(this.DataGridAllJobs); var columnHeadersPresenter = this.DataGridAllJobs.SafeFindDescendant<DataGridColumnHeadersPresenter>(ip => ip.Name == "PART_ColumnHeadersPresenter"); if (columnHeadersPresenter != null) { columnHeadersPresenter.ContextMenu = contextMenu; } 

और यहाँ SafeFindDescendant विस्तार विधि है। मेरी इतनी कड़ी मेहनत से 50 अंक अर्जित हुए ..) आपने तब तक इंतजार क्यों किया जब तक मैं इसे बक्षीस के साथ पोस्ट नहीं करता? =) आपके लिए Thx जवाब!) – MikroDel

+1

क्षमा करें मैंने इसे जल्द से जल्द नहीं देखा :) मैं जितना संभव हो उतना WPF प्रश्नों का उत्तर देने का प्रयास करता हूं, लेकिन सभी का जवाब नहीं दे सकता। हालांकि मैं हमेशा बक्षीस पर एक नज़र डालता हूं :) अगर आपकी समस्या हल हो जाती है तो जवाब के रूप में चिह्नित करना न भूलें! – Sisyphe

+0

आपके प्रश्न को ऊपर उठाया क्योंकि यह interresting था। यह आपके बक्षीस के लिए 10rep कम खो जाएगा;) – Sisyphe

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^