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.

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.

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