An item that does not change the group in the grouped list
I have a listView in which I am showing a collection of vehicles that are grouped by their MaintenanceState. If the MaintenanceState Vehicle update is updated, I expect it to change the group. The collection itself has been updated correctly, however the view is not being updated accordingly. Below is some of my code, maybe someone can help me get this to work.
This is my CollectionViewSource managing my groups
<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 am doing 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 add, remove, or replace Vehicles. MaintenanceStateEnum has the following values: InMaintenance, MarkedForMaintenance and Running.
This is what my car 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 is:
If I have a Vehicle (3, MaintenanceStateEnum.MarkedForMaintenace) and it upgrades to Vehicle (3, MaintenanceStateEnum.InMaintenance), it does not change from MarkedForMaintenance grouping to InMaintenance grouping. Interestingly, it is removed from the MarkedForMaintenance group (the view even leaves space as if the object is still there).
Does anyone know how I can fix my problem?
source to share