Numbered listbox

后端 未结 5 408
闹比i
闹比i 2020-11-28 09:11

I have a sorted listbox and need to display each item\'s row number. In this demo I have a Person class with a Name string property. The listbox displays a a list of Persons

5条回答
  •  粉色の甜心
    2020-11-28 09:15

    The idea in David Brown's link was to use a value converter which worked. Below is a full working sample. The list box has row numbers and can be sorted on both name and age.

    XAML:

    
    
        
    
            
    
            
    
        
    
        
            
                
                
            
            
                
                
            
            
                
                    
                        
                            
                            
                            
                        
                    
                
            
            

    Code behind:

    using System;
    using System.Collections.ObjectModel;
    using System.Windows.Data;
    using System.Windows;
    using System.ComponentModel;
    using System.Windows.Controls;
    
    namespace NumberedListBox
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                Persons = new ObservableCollection();
                Persons.Add(new Person() { Name = "Sally", Age = 34 });
                Persons.Add(new Person() { Name = "Bob", Age = 18 });
                Persons.Add(new Person() { Name = "Joe", Age = 72 });
                Persons.Add(new Person() { Name = "Mary", Age = 12 });
    
                CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
                view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
    
                DataContext = this;
            }
    
            public ObservableCollection Persons { get; private set; }
    
            private void SortButton_Click(object sender, RoutedEventArgs e)
            {
                Button button = sender as Button;
                string sortProperty = button.Tag as string;
                CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
                view.SortDescriptions.Clear();
                view.SortDescriptions.Add(new SortDescription(sortProperty, ListSortDirection.Ascending));
    
                view.View.Refresh();
            }
        }
    
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
    

    Value converter:

    using System;
    using System.Windows.Data;
    
    namespace NumberedListBox
    {
        public class RowNumberConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                CollectionViewSource collectionViewSource = parameter as CollectionViewSource;
    
                int counter = 1;
                foreach (object item in collectionViewSource.View)
                {
                    if (item == value)
                    {
                        return counter.ToString();
                    }
                    counter++;
                }
                return string.Empty;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

提交回复
热议问题