0

I have problems to understand the CollectionView. I implemented it with some sorting/grouping. But it is only sorting the first time the view is created.

View:

<DataGrid ItemsSource="{Binding varCollectionview}" Grid.Row="2" AutoGenerateColumns="False" SelectedItem="{Binding selVariable}" Grid.ColumnSpan="2">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding Name.value}" Foreground="White" Background="Gray" HorizontalAlignment="Stretch" TextAlignment="Left" Padding="10,0" />
                                        <ItemsPresenter></ItemsPresenter>
                                    </StackPanel>

                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                    </Style>
                </GroupStyle.ContainerStyle></GroupStyle>
        </DataGrid.GroupStyle>
        <i:Interaction.Behaviors>
            <behavior:DataGridScrollIntoViewBehavior />
        </i:Interaction.Behaviors>
        <DataGrid.Columns>
        ...
        ...

the MainViewModel call:

variablenVm = new VariablenViewModel(cfg);
  • it is called everytime i load other values etc. for reloading the Tab.

the actual ViewModel for the TabItem:

public class VariablenViewModel : ViewModelBase
{
    private string _filterString;
    public string filterString 
    {
        get { return _filterString ; }
        set
        {
            _filterString = value.ToLower();
            RaisePropertyChanged("filterString ");
            varCollectionview.Refresh();
        }
    }
    private SimuVariable _selVariable;
    public SimuVariable selVariable
    {
        get { return _selVariable; }
        set
        {
            _selVariable = value;
            RaisePropertyChanged("selVariable");
        }
    }
    private Konfiguration _cfg;
    public Konfiguration cfg
    {
        get { return _cfg; }
        set
        {
            _cfg = value;
            RaisePropertyChanged("cfg");
        }
    }
    private ICollectionView _varCollectionview;
    public ICollectionView varCollectionview
    {
        get { return _varCollectionview; }
        set
        {
            _varCollectionview = value;
            RaisePropertyChanged("varCollectionview");
        }
    }

    public VariablenViewModel(Konfiguration _mainCfg)
    {
        cfg = _mainCfg;
        hasSomeValueChanged = false;
        _filterString = "";

        _neueVariableCommand = new RelayCommand(() =>
        {
...
        });
        _loescheVariableCommand = new RelayCommand(
...
        );
        varCollectionview = (CollectionView)CollectionViewSource.GetDefaultView(cfg.variablen);
        if (varCollectionview.GroupDescriptions.Count == 0)
        {
            varCollectionview.GroupDescriptions.Add(new PropertyGroupDescription("varType"));
        }
        if (varCollectionview.SortDescriptions.Count == 0)
        {
            varCollectionview.SortDescriptions.Add(new SortDescription("varType", ListSortDirection.Descending));
            varCollectionview.SortDescriptions.Add(new SortDescription("name", ListSortDirection.Ascending));
        }
        varCollectionview.Filter = new Predicate<object>(filter);
        varCollectionview.Refresh();
    }

    ...
    }

edit: When i first look at the view, the collection is sorted. If i create a new Viewmodel after that, sorting doesnt happen.

After some Debugging i found out that the Sortdescriptions get lost. In the Constructor i add 2 Sortdescription and 1 groupdescription. Some time later i have only the groupdescription left and the sorts are gone and i don't know why :(

https://i.stack.imgur.com/4HYp0.jpg -somewhere the SortDescriptions are lost/deleted

Another question, is it possible to add a SortDescription like varType.id?

  • What do you mean you keep adding `SortDescription`? – Aron Jan 09 '15 at 10:06
  • 1
    @Default: not clear doesnt change the behavior, just tested ->if (varCollectionview.SortDescriptions.Count == 0) -> add – Michael Spannbauer Jan 09 '15 at 10:14
  • @Aron: hmm this behavior seems to be gone. But someday, after calling GetDefaultView i had the sortdescriptions from the last viewmodel instance. But this doesn't change the fact that the sortdescription only has effect on first time creating this ViewModel. – Michael Spannbauer Jan 09 '15 at 10:19
  • refresh doesn't help. I noticed that the sorting is correct the first time i "look" at the View, even if the viewmodel changed a few times. After the view is created, when i change the viewmodel it doesn't sort – Michael Spannbauer Jan 09 '15 at 10:30
  • Could you provide some more code? Complete constructor code might be helpful. Maybe even the whole class - at least the members that are related to the CollectionView. – gehho Jan 09 '15 at 10:48
  • @gehho i added relevant code – Michael Spannbauer Jan 09 '15 at 11:04
  • Mmmh, the code seems to be correct. However, CollectionViews are sometimes strange. What happens if you replace `CollectionViewSource.GetDefaultView` with `new ListCollectionView`? Instead of getting the default view (which will always be the same if `cfg.variablen` is the same instance), this creates a new CollectionView every time. – gehho Jan 09 '15 at 11:16
  • Another idea is to get rid of those `SortDescriptions` (which are pretty slow if you have lots of items due to reflection) and instead implement `IComparable` in your items, and then set `ListCollectionView.CustomSort = Comparer.Default`. Of course you could also implement your own custom `IComparer` if you prefer that. – gehho Jan 09 '15 at 11:19
  • @gehho: varCollectionview = new ListCollectionView(cfg.variablen) has the same behavior :( .. Thanks for the tip with IComparable, i will notice it when creating a new project, but here i have ~50 items so i think its not worth. – Michael Spannbauer Jan 09 '15 at 11:31
  • After some Debugging i found out that the Sortdescriptions get lost. In the Construktor i add 2 Sortdescription and 1 groupdescription Some time later i have only the groupdescription left and the sorts are gone :( – Michael Spannbauer Jan 12 '15 at 09:28
  • solved. look own answer below – Michael Spannbauer Jan 12 '15 at 09:59

1 Answers1

0

Hell yeah, i found a Solution.

varCollectionview = new CollectionViewSource();
        varCollectionview.Source = cfg.variablen;
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(delegate()
            {
        varCollectionview.View.SortDescriptions.Add(new SortDescription("varType", ListSortDirection.Descending));
        varCollectionview.View.SortDescriptions.Add(new SortDescription("name", ListSortDirection.Ascending));
        varCollectionview.View.GroupDescriptions.Add(new PropertyGroupDescription("varType"));
        varCollectionview.View.Filter = new Predicate<object>(filter);
        varCollectionview.View.Refresh();
            }));

it seems that the View is noticed that the source is updated and deletes the sortdescriptions, but that happes delayed. All to do is add the sortdescription also delayed