-2

I'm using the CollectionViewSource with LiveShaping properties, especially Grouping activated. The problem is that changing the Grouping properties does not work immediately. It works however, when I refresh the view by manually calling Refresh on the View.

But the question is then, why do I have the LiveShaping properties on the CollectionViewSource if they do not work? Or I'm I doing something wrong?

Here is an example:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Data;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            ObservableCollection<Test> TestList = new ObservableCollection<Test>();

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Test t = new Test()
                    {
                        Group = i.ToString(),
                        Name = (j + (i * 10)).ToString()
                    };

                    TestList.Add(t);
                }
            }

            CollectionViewSource aViewSource = new CollectionViewSource();
            aViewSource.Source = TestList;

            aViewSource.IsLiveGroupingRequested = true;
            aViewSource.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
            aViewSource.LiveGroupingProperties.Add("Group");

            foreach (CollectionViewGroup o in aViewSource.View.Groups)
            {
                Console.WriteLine(o.Name);
                foreach (Test o2 in o.Items)Console.WriteLine("     " + o2.Name);
            }

            Console.WriteLine("----------------------------------------------");


            TestList[4].Group = "4";

            //aViewSource.View.Refresh(); When using this line it works.

            foreach (CollectionViewGroup o in aViewSource.View.Groups)
            {
                Console.WriteLine(o.Name);
                foreach (Test o2 in o.Items) Console.WriteLine("     " + o2.Name);
            }

            Console.WriteLine("----------------------------------------------");


            foreach (Test test in TestList)
            {
                Console.WriteLine("{0}: {1}", test.Group, test.Name);
            }


            Console.WriteLine("----------------------------------------------");
            Console.ReadKey();
        }
    }

    public class Test : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string myName;
        private string myGroup;

        public string Name
        {
            get
            {
                return myName;
            }

            set
            {
                myName = value;
                OnPropertyChanged();
            }
        }

        public string Group
        {
            get
            {
                return myGroup;
            }

            set
            {
                myGroup = value;
                OnPropertyChanged();
            }
        }


        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName_in = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName_in));
        }

        public override string ToString()
        {
            return $"{Group}: {Name}";
        }
    }
}
msedi
  • 1,437
  • 15
  • 25

1 Answers1

0

A CollectionViewSource with live grouping enabled doesn't work in the context of a console application where there is no dispatcher available for the same reasons as being mentioned in the answers to your other question:

CollectionView without a dispatcher

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Understood. But when using the same code in a WPF window the same code also does only work if I explicitly call the View.Refresh() method or if connect iot as an ItemsSource to a e.g. ListBox. – msedi Apr 12 '17 at 07:56
  • Please provide a WPF example of your issue then. – mm8 Apr 12 '17 at 08:44