2011-06-24 22 views
14

में एकाधिक कॉलम पर सॉर्ट करें मैं अपने WPF डेटाग्रिड को दो क्रमबद्ध कॉलम के समान कॉलम पर सेट करने के लिए कैसे सेट कर सकता हूं, प्राथमिक प्रकार के पहले कॉलम के शीर्षलेख पर क्लिक करके और उसके बाद हेडर पर SHIFT क्लिक करना माध्यमिक क्रम के लिए दूसरा कॉलम। मैं एकाधिक कॉलम सॉर्ट स्वचालित रूप से तब होता हूं जब उपयोगकर्ता पहले कॉलम के शीर्ष पर क्लिक करता है बिना SHIFT को दूसरे कॉलम हेडर पर क्लिक करता है। क्या यह पूरी तरह से xaml में ऐसा करने का कोई तरीका है? यदि नहीं, तो मैं कोड के पीछे यह कैसे कर सकता हूं? वर्तमान में वीबी.Net का उपयोग कर रहा है लेकिन यदि आपके पास एक सी # स्निपेट स्वीकार्य है। धन्यवाद!WPF डेटाग्रिड

उत्तर

17

आप इस तरह System.ComponentModel नाम स्थान जोड़ कर ऐसा कर सकते हैं:

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 

तो CollectionViewSource XAML के अंदर इस तरह नई SortDescriptions जोड़ने:

<CollectionViewSource … > 
      <CollectionViewSource.SortDescriptions> 
       <scm:SortDescription PropertyName="Column1"/> 
       <scm:SortDescription PropertyName="Column2"/> 
      </CollectionViewSource.SortDescriptions> 
</CollectionViewSource> 

इस वसीयत कॉलम 1, कॉलम 2 पर डेटाग्रिड को सॉर्ट करें।

संपादित करें:

भी पीछे इस का उपयोग करते हुए सी # कोड कर बहुत आसान है:

private void btnSort_Click(object sender, RoutedEventArgs e) 
    { 
     System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("The_ViewSource_Name"))); 
     myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending)); 
     myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending)); 
    } 

EDIT2:

वर्कअराउंड स्तंभ शीर्ष लेख के लिए छोड़ दिया माउस क्लिक पकड़ने के लिए बनाया जा सकता है घटना और ग्रिड को उस कॉलम पर क्रमबद्ध करने से रोकें:

  • ग्रिड संपत्ति अक्षम नामित CanUserSortColumns

enter image description here

  • ग्रिड PreviewMouseLeftButtonUp घटना के लिए इस कोड जोड़ें:

    private void myDataGrid_PreviewMouseLeftButtonUp(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; 
         // check if this is the wanted column 
         if (columnHeader.Column.Header.ToString() == "The_Wanted_Column_Title") 
         { 
          System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource"))); 
          myViewSource.SortDescriptions.Clear(); 
          myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending)); 
          myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending)); 
         } 
         else 
         { 
          //usort the grid on clicking on any other columns, or maybe do another sort combination 
          System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource"))); 
          myViewSource.SortDescriptions.Clear(); 
         } 
    
        } 
    } 
    

आप अपनी आवश्यकताओं को प्राप्त करने के लिए इस कोड को संशोधित और विस्तृत कर सकते हैं।

+0

ओपी ने पूछा कि उपयोगकर्ता को एकाधिक कॉलम हेडर पर क्लिक करके ग्रिड को सॉर्ट करने की अनुमति कैसे दी जाए, न कि अंतर्निहित संग्रह को प्रोग्रामेटिक रूप से कैसे क्रमबद्ध करें। –

+0

@djacobson मुझे लगता है कि मैंने प्रश्न अच्छी तरह पढ़ा है, वह लिखते हैं: "क्या यह पूरी तरह से xaml में ऐसा करने का कोई तरीका है?" –

+0

@djacobson कई कॉलम हेडर पर क्लिक करके ग्रिड स्वचालित रूप से चयनित कॉलम पर सॉर्ट होता है। –

11

मुझे आशा है कि इससे दूसरों की मदद मिलेगी। मेरा समाधान डिफ़ॉल्ट प्रकार की कार्यक्षमता रखता है और एकाधिक कॉलम पर सॉर्टिंग की अनुमति देता है।

पीछे

private void dataGridName_Sorting(object sender, DataGridSortingEventArgs e) 
{ 
    var dgSender = (DataGrid) sender; 
    var cView = CollectionViewSource.GetDefaultView(dgSender.ItemsSource); 

    //Alternate between ascending/descending if the same column is clicked 
    ListSortDirection direction = ListSortDirection.Ascending; 
    if (cView.SortDescriptions.FirstOrDefault().PropertyName == e.Column.SortMemberPath) 
     direction = cView.SortDescriptions.FirstOrDefault().Direction == ListSortDirection.Descending ? ListSortDirection.Ascending : ListSortDirection.Descending; 

    cView.SortDescriptions.Clear(); 
    AddSortColumn((DataGrid)sender, e.Column.SortMemberPath, direction); 
    //To this point the default sort functionality is implemented 

    //Now check the wanted columns and add multiple sort 
    if (e.Column.SortMemberPath == "WantedColumn") 
    { 
     AddSortColumn((DataGrid)sender, "SecondColumn", direction); 
    } 
    e.Handled = true; 
} 

private void AddSortColumn(DataGrid sender, string sortColumn, ListSortDirection direction) 
{ 
    var cView = CollectionViewSource.GetDefaultView(sender.ItemsSource); 
    cView.SortDescriptions.Add(new SortDescription(sortColumn, direction)); 
    //Add the sort arrow on the DataGridColumn 
    foreach (var col in sender.Columns.Where(x => x.SortMemberPath == sortColumn)) 
    { 
     col.SortDirection = direction; 
    } 
} 

अपने कोड में अपने datagrid

<DataGrid x:Name="dataGridName" Sorting="dataGridName_Sorting"> 

पर एक छँटाई घटना रखो और अब sortDirection DataGridColumn पर ग्रिड पर तीर दिखा अनुमति देते हैं।

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