Reputation: 193
I have a WPF ListView
which has a List<T>
as ItemsSource
.
Now I want to query the list name which is bind to the ListView
. How do I achieve that?
I need the name of the List<T>
so I can reuse the function. Example:
private void Sort(ListView lv)
{
lv.ItemsSource.Sort((x, y) => y.Name.CompareTo(x.Name)); // does not work
}
Upvotes: 0
Views: 173
Reputation: 12276
If you take a look at the code here:
https://www.wpf-tutorial.com/listview-control/listview-sorting/
You will notice you don't need the name of the list to sort it.
EDIT: And you can reference a column using just a generic string which seeems to be "Name" in your case.
The listview works with the default view of what's set or bound to it's itemssource. You can grab a reference to that and apply sort descriptors.
Code from the link:
public ListViewSortingSample()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42 });
items.Add(new User() { Name = "Jane Doe", Age = 39 });
items.Add(new User() { Name = "Sammy Doe", Age = 13 });
items.Add(new User() { Name = "Donna Doe", Age = 13 });
lvUsers.ItemsSource = items;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
view.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Ascending));
}
Substitute "Name" for age in that sortdescription and you'll have it sorting on the Name property.
Upvotes: 1