How to sort two ListViews with the same source independently

Marco Handl 21 Reputation points
2022-06-13T04:13:28.48+00:00

Hello guys,

I need some help.

I have one singleton class who holds a ObservableCollection.
Then I have two Windows each has a ListView. The ItemsSource of the views is the ObservableCollection from the Singelton Class.
All will be shown and it works fine.

But, if I sort one ListView on Window A, the other ListView on Window B gets also sorted. Is there a way that I can separatly sort both ListViews without copying the initial collection?

here my initial linking:

//creating the singelton object  
acController = AcController.CreateInstance(m_UAClientHelperAPI);  
//Create the view  
view = (ListCollectionView)CollectionViewSource.GetDefaultView(acController.AlarmList);  
//Sort view  
view.SortDescriptions.Add(new SortDescription("Time", ListSortDirection.Ascending));  
view.Refresh();  
//connect view to ListView  
AlarmListView.ItemsSource = view;  

here the main part of the sorting when I press a headerbutton:

        private void Sort(string sortBy, ListSortDirection direction) {  
            ICollectionView dataView =  
              CollectionViewSource.GetDefaultView(AlarmListView.ItemsSource);  
  
            dataView.SortDescriptions.Clear();  
            SortDescription sd = new SortDescription(sortBy, direction);  
            dataView.SortDescriptions.Add(sd);  
            dataView.Refresh();  
        }  

Thanks in advance!

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-06-13T06:02:36.913+00:00

    Hi Marco,
    you can use 2 different CollectionViewSources and sort each View (of CollectionViewSource) with other SortDescriptions like in following demo:

    <Window x:Class="WpfApp1.Window025"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp025"  
            mc:Ignorable="d"  
            Title="MarcoHandl-3158_20220613" Height="450" Width="400">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <StackPanel>  
        <ListView ItemsSource="{Binding View}" Height="200">  
          <ListView.View>  
            <GridView>  
              <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="100"/>  
              <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Time}" Width="200"/>  
            </GridView>  
          </ListView.View>  
        </ListView>  
        <ListView ItemsSource="{Binding DataView}" Height="200">  
          <ListView.View>  
            <GridView>  
              <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="100"/>  
              <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Time}" Width="200"/>  
            </GridView>  
          </ListView.View>  
        </ListView>  
      </StackPanel>  
    </Window>  
    

    ViewModel:

    using System;  
    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace WpfApp025  
    {  
      public class ViewModel  
      {  
        AcController acController;  
        public ViewModel()  
        {  
          //creating the singelton object  
          acController = AcController.CreateInstance();  
          //Create the view  
          cvsView.Source = acController.AlarmList;  
          //Sort view  
          cvsView.View.SortDescriptions.Add(new SortDescription("Time", ListSortDirection.Ascending));  
          //connect view to ListView  
          // -> Binding!  
          cvsDataView.Source = acController.AlarmList;  
      
          DataView.SortDescriptions.Clear();  
          SortDescription sd = new SortDescription("Id", ListSortDirection.Descending);  
          DataView.SortDescriptions.Add(sd);  
          //dataView.Refresh();  
        }  
        private CollectionViewSource cvsView = new CollectionViewSource();  
        public ICollectionView View { get => cvsView.View; }  
      
        private CollectionViewSource cvsDataView = new CollectionViewSource();  
        public ICollectionView DataView { get => cvsDataView.View; }  
      }  
    
      internal class AcController  
      {  
    ...  
      }  
    
      public class Data  
      {  
        public int Id { get; set; }  
        public DateTime Time { get; set; }  
      }  
    }  
    

    210657-x.png

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.