Sorting a list when a button is clicked using MVVM

I have been looking for a solution to my problem since yesterday. I am creating a problem with the MVVM pattern. I have two user controls that contain a list.

The first usercontrol is called SearchView, which contains a list of project names that the user can select and save in local db applications.

enter image description here

When you add selected projects, an event is fired that notifies the second user control called ProjectView. This usercontrol simply shows which projects are saved locally. See the figure below.

enter image description here

The problem is that I want to sort the list in ascending order by name in projectview. Thus, if the user first adds β€œTest Project 2,” and the afterword adds β€œTest Project 1,” β€œTest Project 1” will be shown at the top of the list.

I tried using ICollectionView and ListCollectionView, but at the moment they are very confused.

So now my code looks like this in ProjectViewModel, which should sort the list:

public ProjectViewModel() { this.collectionView = CollectionViewSource.GetDefaultView(this.Projects); } private ObservableCollection<ProjectWrapper> _project = new ObservableCollection<ProjectWrapper>(); public ObservableCollection<ProjectWrapper> Projects { get { return _project; } set { _project = value; OnPropertyChanged("Projects"); } } 

XAML Code:

 <UserControl.Resources> <CollectionViewSource x:Key="cvs" Source="{Binding Path=Projects}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="ProjectWrapper.Project.Name" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource> </UserControl.Resources> <ListBox Name="ProjectsList" ItemsSource="{Binding Source={StaticResource cvs}}" SelectedItem="{Binding Path=SelectedProject}" HorizontalContentAlignment="Stretch" BorderThickness="0" Grid.Row="1" Grid.RowSpan="3" Margin="0,0.4,-0.2,27.8"> <ListBox.ItemTemplate> <DataTemplate> <DockPanel> <TextBlock Text="{Binding Path=ProjectModel.Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Padding="3,2,0,0" /> <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="0,2,5,0" Margin="0,2.5,0,0" /> </DockPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

Thank you in advance

+6
source share
3 answers

call this by adding an item to the Projects list

 Projects = new ObservableCollection<ProjectWrapper>(Projects.OrderBy(x => x.Name)); 
+2
source

you can use linq

Example:

  var coll = new ObservableCollection<myItem>(); coll.Add(new myItem() { Id = 0, Name = "zz" }); coll.Add(new myItem() { Id = 3, Name = "bb" }); coll.Add(new myItem() { Id = 1, Name = "aa" }); var sortedById = from item in coll orderby item.Id select item; var sortedByName = from item in coll orderby item.Name select item; coll = new ObservableCollection<myItem>(sortedById); coll = new ObservableCollection<myItem>(sortedByName); class myItem { public int Id { get; set; } public string Name { get; set; } } 
0
source

This should be enough if you change the SortDescription in your XAML to:

 <scm:SortDescription PropertyName="ProjectModel.Name" /> 

Since you want to sort by project name, where I think this is the same as what you use in the TextBlock binding {Binding Path=ProjectModel.Name}

0
source

Source: https://habr.com/ru/post/944196/


All Articles