में एकाधिक कॉलम पर सॉर्ट करें मैं अपने WPF डेटाग्रिड को दो क्रमबद्ध कॉलम के समान कॉलम पर सेट करने के लिए कैसे सेट कर सकता हूं, प्राथमिक प्रकार के पहले कॉलम के शीर्षलेख पर क्लिक करके और उसके बाद हेडर पर SHIFT क्लिक करना माध्यमिक क्रम के लिए दूसरा कॉलम। मैं एकाधिक कॉलम सॉर्ट स्वचालित रूप से तब होता हूं जब उपयोगकर्ता पहले कॉलम के शीर्ष पर क्लिक करता है बिना SHIFT को दूसरे कॉलम हेडर पर क्लिक करता है। क्या यह पूरी तरह से xaml में ऐसा करने का कोई तरीका है? यदि नहीं, तो मैं कोड के पीछे यह कैसे कर सकता हूं? वर्तमान में वीबी.Net का उपयोग कर रहा है लेकिन यदि आपके पास एक सी # स्निपेट स्वीकार्य है। धन्यवाद!WPF डेटाग्रिड
उत्तर
आप इस तरह 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
ग्रिड 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(); } } }
आप अपनी आवश्यकताओं को प्राप्त करने के लिए इस कोड को संशोधित और विस्तृत कर सकते हैं।
मुझे आशा है कि इससे दूसरों की मदद मिलेगी। मेरा समाधान डिफ़ॉल्ट प्रकार की कार्यक्षमता रखता है और एकाधिक कॉलम पर सॉर्टिंग की अनुमति देता है।
पीछे
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 पर ग्रिड पर तीर दिखा अनुमति देते हैं।
- 1. डेटाग्रिड (WPF)
- 2. डेटाग्रिड-WPF
- 3. wpf डेटाग्रिड
- 4. wpf डेटाग्रिड
- 5. एक WPF डेटाग्रिड
- 6. एक WPF डेटाग्रिड
- 7. WPF डेटाग्रिड प्रदर्शन चिंताओं
- 8. बड़े wpf डेटाग्रिड
- 9. एक WPF डेटाग्रिड
- 10. WPF डेटाग्रिड पूर्वावलोकनमाउसडाउन
- 11. एक्सेल से WPF डेटाग्रिड
- 12. wpf हटाएं डेटाग्रिड बाएं पैडिंग
- 13. WPF डेटाग्रिड पंक्ति रंग बदलें
- 14. WPF टूलकिट नियंत्रण (विशेष रूप से डेटाग्रिड)
- 15. स्टाइल या संसाधन में WPF डेटाग्रिड कॉलम
- 16. WPF डेटाग्रिड बाध्यकारी कस्टम कॉलम हेडर
- 17. wpf डेटाग्रिड सेल शैली के साथ समस्या
- 18. WPF डेटाग्रिड सेल रंग पूर्व सेल मूल्य
- 19. wpf डेटाग्रिड में संदर्भ मेनू कैसे जोड़ें?
- 20. WPF डेटाग्रिड पंक्ति क्षैतिज संरेखण समस्या
- 21. डेटाग्रिड
- 22. डेटाग्रिड
- 23. डेटाग्रिड
- 24. डब्ल्यूपीएफ: डेटाग्रिड
- 25. WPF में सूची दृश्य और डेटाग्रिड के बीच अंतर?
- 26. मैं WPF डेटाग्रिड पर स्क्रॉलबार कैसे सक्षम कर सकता हूं?
- 27. एक WPF डेटाग्रिड पृष्ठभूमि छवि को चालू और बंद करना?
- 28. WPF डेटाग्रिड की ऊंचाई बढ़ जाती है जब डेटा
- 29. WPF डेटाग्रिड समस्याओं की सूची के लिए बाध्यकारी
- 30. कुछ अच्छे WPF डेटाग्रिड उपयोग उदाहरण क्या हैं?
ओपी ने पूछा कि उपयोगकर्ता को एकाधिक कॉलम हेडर पर क्लिक करके ग्रिड को सॉर्ट करने की अनुमति कैसे दी जाए, न कि अंतर्निहित संग्रह को प्रोग्रामेटिक रूप से कैसे क्रमबद्ध करें। –
@djacobson मुझे लगता है कि मैंने प्रश्न अच्छी तरह पढ़ा है, वह लिखते हैं: "क्या यह पूरी तरह से xaml में ऐसा करने का कोई तरीका है?" –
@djacobson कई कॉलम हेडर पर क्लिक करके ग्रिड स्वचालित रूप से चयनित कॉलम पर सॉर्ट होता है। –