0

So, I am making a List of UserControl which ViewModel is assigned via ItemsControl

<ItemsControl Grid.Column="0" Grid.Row="1" x:Name="itemsControlStack"             
     ItemsSource="{Binding ListOfViewModels, Mode=OneWay}" VerticalAlignment="Bottom">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:UserControlA/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Above code works, but I want to show it reversed, without changing the data, so :

<ItemsControl Grid.Column="0" Grid.Row="1" x:Name="itemsControlStack"
    ItemsSource="{Binding REVERSEDListOfViewModels, Mode=OneWay}" VerticalAlignment="Bottom">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:UserControlA/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Somewhere in my ViewModel, which I have checked via breakpoint that the code is executed

public ICollectionView REVERSEDListOfViewModels { get; private set; }

REVERSEDListOfViewModels = CollectionViewSource.GetDefaultView(ListOfViewModels);
REVERSEDListOfViewModels.SortDescriptions.Add(new SortDescription("ID", ListSortDirection.Descending));

But it suddenly not working. it doesn't return error anywhere, but UserControlA now don't properly presented. (The user control are presented without its data - which stored on the REVERSEDListOfViewModels)

Any idea why?

Moses Aprico
  • 1,951
  • 5
  • 30
  • 53
  • What is `REVERSEDListOfViewModels`? Can you show how this property is defined? – dkozl Oct 29 '14 at 09:33
  • 1
    `INotifyPropertyChanged`? you need to fire PropertyChanged event in the setter of REVERSEDListOfViewModels. – kennyzx Oct 29 '14 at 09:39
  • 1
    @MosesAprico it seems that `REVERSEDListOfViewModels` does not raise property changed event. When do you set it? – dkozl Oct 29 '14 at 09:40
  • Even after I set it, nothing happens. Btw, isn't it the actual Collection which is changed? I already set it in the ListOfViewModel – Moses Aprico Oct 29 '14 at 10:44
  • You are overwriting the value of `REVERSEDListOfViewModel` with a new `ICollectionView`, and the binding doesn't know about it because you're not raising `PropertyChanged` in the setter. The `ItemsSource` therefore never points to the new collection view. However, since you're just adding a sort descriptor to the default collection view, you could just keep the original binding to `ListOfViewModels`, and the sorting should still be reflected as long as you sort the default collection view. You don't actually need to store the view in another property. – Mike Strobel Oct 29 '14 at 19:20
  • Yeah I've got it. Basically I need 1 Notify Reversed on itself and on ListOfVMs – Moses Aprico Oct 29 '14 at 19:22

0 Answers0