I have a DataGrid
bound to a list of a simple object and a Checkbox
that I want to set/unset the Selected field of those objects in the list.
The code below does not work. Clicking the Select All checkbox changes the data but the grid is not updated. However, if I change
get => this._ordersToTransmit;
to
get => this._ordersToTransmit.ToList();
then the grid IS updated and it works properly.
Can anyone explain to me why I need to put the ToList()
there? Maybe I should be doing something entirely different?
class ViewModel
{
private List<OrderListItem> _ordersToTransmit;
public List<OrderListItem> OrdersToTransmit
{
get => this._ordersToTransmit;
set => this.SetProperty(ref this._ordersToTransmit, value);
}
public bool SelectAll
{
get => this._selectAll;
set
{
this.SetProperty(ref this._selectAll, value);
foreach (OrderListItem item in this._ordersToTransmit)
item.Selected = value;
this.RaisePropertyChanged(nameof(TransmitOrdersViewModel.OrdersToTransmit));
}
}
}
<CheckBox Content="Select All" IsChecked="{Binding SelectAll, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGrid ItemsSource="{Binding OrdersToTransmit}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Selected}" />
</DataGrid.Columns>
</DataGrid>