2010-01-25 15 views
20

जब उपयोगकर्ता मेरे DataGrid में कॉलम सॉर्ट करता है, तो मैं शीर्ष की बजाय सभी शून्य या खाली कोशिकाओं को नीचे क्रमबद्ध करना चाहता हूं।मैं एक WPF DataGrid पर एक कस्टम सॉर्ट नियम कैसे लागू कर सकता हूं?

मैंने IComparer<T> लिखा है जो सुनिश्चित करता है कि रिक्त स्थान हमेशा नीचे की ओर क्रमबद्ध होते हैं, लेकिन मैं यह नहीं समझ सकता कि इसे मेरे DataGrid के कॉलम पर कैसे लागू किया जाए। ध्यान दें कि प्रारंभिकDataGrid का प्रकार, जो मैं LINQ OrderBy() विधि के साथ कर रहा हूं, बहुत अच्छा काम करता है। समस्या यह है कि उपयोगकर्ता द्वारा किए गए सभी प्रकार के रिक्त स्थान शीर्ष पर रिक्त स्थान को क्रमबद्ध करते हैं।

comparer कोड

public class BlankLastStringComparer : IComparer<string> 
{ 
    public int Compare(string x, string y) 
    { 
     if (string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y)) 
      return 1; 
     else if (!string.IsNullOrEmpty(x) && string.IsNullOrEmpty(y)) 
      return -1; 
     else 
      return string.Compare(x, y); 
    } 
} 

प्रश्न

मैं कैसे DataGridColumn मेरी comparer उपयोग करने के लिए मिलता है? या यदि यह संभव नहीं है, तो क्या आप एक कामकाज की पेशकश कर सकते हैं? यदि संभव हो तो मैं एक एमवीवीएम अनुकूल समाधान की उम्मीद कर रहा हूं।

उत्तर

22

यह है कि कैसे मैं यह कर: मैं ग्रिड से निकाले जाते हैं करते वर्ग के भीतर यह सब रखने के लिए, तो मैं घटना संचालकों को देते हैं आंतरिक

छँटाई घटना से संलग्न

dataGrid.Sorting += new DataGridSortingEventHandler(SortHandler); 

लागू विधि (मैं एक व्युत्पन्न वर्ग में ऐसा)

void SortHandler(object sender, DataGridSortingEventArgs e) 
{ 
    DataGridColumn column = e.Column; 

    IComparer comparer = null; 

    //i do some custom checking based on column to get the right comparer 
    //i have different comparers for different columns. I also handle the sort direction 
    //in my comparer 

    // prevent the built-in sort from sorting 
    e.Handled = true; 

    ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending; 

    //set the sort order on the column 
    column.SortDirection = direction; 

    //use a ListCollectionView to do the sort. 
    ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.ItemsSource); 

    //this is my custom sorter it just derives from IComparer and has a few properties 
    //you could just apply the comparer but i needed to do a few extra bits and pieces 
    comparer = new ResultSort(direction); 

    //apply the sort 
    lcv.CustomSort = comparer; 
} 
+0

बस एक नोट संग्रह अपने डेटा ग्रिड के लिए बाध्य किया गया था के प्रकार पर निर्भर करता है, आप के लिए [एक अलग प्रकार] GetDefaultView का परिणाम कास्ट करने के लिए हो सकता है (http://msdn.microsoft.com/ en-us/पुस्तकालय/ms752347.aspx # how_to_create_a_view)। – xr280xr

3

मैं इस समस्या को जो संलग्न व्यवहार का उपयोग करता है के लिए एक MVVM समाधान है। यदि आप कोड-बैक का उपयोग करना पसंद करते हैं, तो @ अरन का समाधान भी चाल करेगा।

https://stackoverflow.com/a/18218963/2115261

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