3

I have a listView in which I show a collection of Vehicles which are grouped by their MaintenanceState. If the MaintenanceState of the Vehicle updates I expect it to change group. The collection itself is correctly updated, however the view does not update accordingly. Below is some of my code, maybe someone can help me getting this to work.

This is my CollectionViewSource managing my groupings

<CollectionViewSource x:Key="GroupedVehicles" IsLiveGroupingRequested="True" Source="{Binding ItemCollection}">
   <CollectionViewSource.GroupDescriptions>
      <PropertyGroupDescription PropertyName="MaintenanceState" />
   </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

Here is my ListView

    <ListView ItemContainerStyle="{DynamicResource VehicleItemContainerStyle}"
              ItemsSource="{Binding Source={StaticResource GroupedVehicles}}"
              SelectedItem="{Binding SelectedItem}"
              SelectionMode="Single"
              Style="{DynamicResource VehiclesListViewStyle}">
       <ListView.GroupStyle>
          <GroupStyle>
             <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                   <Setter Property="Template">
                      <Setter.Value>
                         <ControlTemplate TargetType="{x:Type GroupItem}">
                            <StackPanel>
                               <Expander Header="{Binding Path=Name}"
                                         IsExpanded="True"
                                         Style="{DynamicResource VehicleListSectionExpanderStyle}">
                                  <ItemsPresenter />
                               </Expander>
                            </StackPanel>
                         </ControlTemplate>
                       </Setter.Value>
                    </Setter>
                 </Style>
              </GroupStyle.ContainerStyle>
           </GroupStyle>
        </ListView.GroupStyle>
        <ListView.ItemTemplate>
           <DataTemplate>
              <TextBlock Text="{Binding Number}" />
           </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

This is what I do on my ViewModel

Vehicle updatedVehicle = new Vehicle(vehicleNumber, MaintenanceStateEnum.Running);
ItemCollection[index] = updatedVehicle;

The ItemCollection is of type ObservableCollection<Vehicle> and I make sure to only add, remove or replace Vehicles. The MaintenanceStateEnum has the following values: InMaintenance, MarkedForMaintenance and Running.

This is what my Vehicle looks like

public class Vehicle
{
    public Vehicle(int number, MaintenanceStateEnum state) {}
    public int Number { get; private set; }
    public MaintenanceStateEnum MaintenanceState { get; private set; }
}

So my problem:

If I have Vehicle(3, MaintenanceStateEnum.MarkedForMaintenace) and it is updated to Vehicle(3, MaintenanceStateEnum.InMaintenance) it does not change from the grouping MarkedForMaintenance to the grouping InMaintenance. Interesting is that it does get removed from the MarkedForMaintenance grouping (the view even leaves a space as if the object is still there).

Does anyone know how I can fix my problem?

1 Answers1

0

I think the issue here is that the view does not know that the collection has changed. You could try to change your container from ItemCollection to ObservableCollection which implements both INotifyCollectionChanged and INotifyPropertyChanged.

Nebula
  • 1,045
  • 2
  • 11
  • 24
  • 'ItemCollection' is the name of the collection, it actually already is of type 'ObservableCollection'. I have the feeling that the View does get the notifications of change, since a grouping gets an empty spot when an object moves from one group to the other, however that object never gets shown into the group it has moved to. I suspect that the way I am working with the groupings (maybe how I am defining them in XAML) is not completely correct, or I am missing something. Any ideas here? – cherryorange Jan 06 '15 at 13:07