2011-01-05 18 views
5

मैं डब्ल्यूपीएफ डाटाग्रिड के साथ काम कर रहा हूं। मुझे कॉलम हैडर खींचना है, इसे किसी अन्य नियंत्रण में छोड़ना है (लेबल कहें) और कुछ ऑपरेशन करें। लेकिन मैं डेटाग्रिड कॉलम हैडर की ड्रैग और ड्रॉप प्राप्त करने में सक्षम नहीं हूं। मैंने ColumnHeaderDragStarted ईवेंट के साथ प्रयास किया है, लेकिन मैं हैंडलर में कॉलम हैडर ऑब्जेक्ट या हेडर का नाम नहीं ढूंढ पा रहा हूं। कोई मदद कृपया !!डेटाग्रिड कॉलम हेडर को कैसे खींचें और छोड़ें?

उत्तर

4

हो सकता है कि यह आपकी मदद कर सकते हैं:

XAML पर:

< डेटा ग्रिड नाम = "डेटा ग्रिड" MouseRightButtonUp = "DataGrid_MouseRightButtonUp"/>

सी # कोड पर:

private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     DependencyObject dep = (DependencyObject)e.OriginalSource; 

     while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     if (dep == null) 
      return; 

     if (dep is DataGridColumnHeader) 
     { 
      DataGridColumnHeader columnHeader = dep as DataGridColumnHeader; 

      // find the property that this cell's column is bound to 
      string boundPropertyName = FindBoundProperty(columnHeader.Column); 

      int columnIndex = columnHeader.Column.DisplayIndex; 

      ClickedItemDisplay.Text = string.Format(
       "Header clicked [{0}] = {1}", 
       columnIndex, boundPropertyName); 
     } 

     if (dep is DataGridCell) 
     { 
      DataGridCell cell = dep as DataGridCell; 

      // navigate further up the tree 
      while ((dep != null) && !(dep is DataGridRow)) 
      { 
       dep = VisualTreeHelper.GetParent(dep); 
      } 

      if (dep == null) 
       return; 

      DataGridRow row = dep as DataGridRow; 

      object value = ExtractBoundValue(row, cell); 

      int columnIndex = cell.Column.DisplayIndex; 
      int rowIndex = FindRowIndex(row); 

      ClickedItemDisplay.Text = string.Format(
       "Cell clicked [{0}, {1}] = {2}", 
       rowIndex, columnIndex, value.ToString()); 
     } 
    } 

    /// <summary> 
    /// Determine the index of a DataGridRow 
    /// </summary> 
    /// <param name="row"></param> 
    /// <returns></returns> 
    private int FindRowIndex(DataGridRow row) 
    { 
     DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid; 

     int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row); 

     return index; 
    } 

    /// <summary> 
    /// Find the value that is bound to a DataGridCell 
    /// </summary> 
    /// <param name="row"></param> 
    /// <param name="cell"></param> 
    /// <returns></returns> 
    private object ExtractBoundValue(DataGridRow row, DataGridCell cell) 
    { 
     // find the property that this cell's column is bound to 
     string boundPropertyName = FindBoundProperty(cell.Column); 

     // find the object that is realted to this row 
     object data = row.Item; 

     // extract the property value 
     PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data); 
     PropertyDescriptor property = properties[boundPropertyName]; 
     object value = property.GetValue(data); 

     return value; 
    } 

    /// <summary> 
    /// Find the name of the property which is bound to the given column 
    /// </summary> 
    /// <param name="col"></param> 
    /// <returns></returns> 
    private string FindBoundProperty(DataGridColumn col) 
    { 
     DataGridBoundColumn boundColumn = col as DataGridBoundColumn; 

     // find the property that this column is bound to 
     Binding binding = boundColumn.Binding as Binding; 
     string boundPropertyName = binding.Path.Path; 

     return boundPropertyName; 
    } 
} 

// This XAML and C# where extracted from a link contained on this URL:  
// http://social.msdn.microsoft.com/Forums/en/wpf/thread/61707b8a-e6c6-474b-ac2b-3446319625bd 
संबंधित मुद्दे