Select all the checkboxes in the datagrid rows from the view model.

I am using Caliburn Micro as an MVVM environment in a WPF application. I have few problems how to select all checkbox in datagrid control. Each row of the datagrid has a checkbox.

I bind to the datagrid property type of the list.

Model:

public class Bill : INotifyPropertyChanged { public string CellPhoneNo { get { return _cellPhoneNo; } set { _cellPhoneNo = value; NotifyPropertyChanged("CellPhoneNo"); } } public bool IsSelected { get { return _isSelected; } set { _isSelected = value; NotifyPropertyChanged("IsSelected"); } } 

ViewModel:

  public IList<Bill> TmobileBill { get { return _tmobileBill; } set { _tmobileBill = value; NotifyOfPropertyChange(()=>TmobileBill); } } 

View:

  <Controls:DataGrid ItemsSource="{Binding Path= TmobileBill, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource FinalBillsView_CallsDataGrid}" Grid.Row="0" CanUserResizeRows="False"> <Controls:DataGrid.RowHeaderTemplate> <DataTemplate> <Grid> <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:DataGridRow}}}"/> </Grid> </DataTemplate> </Controls:DataGrid.RowHeaderTemplate> <Controls:DataGrid.Columns> <Controls:DataGridTextColumn IsReadOnly="True" CellStyle="{StaticResource FinalBillsView_DataGrid_CellStyle}" Binding="{Binding Path=CellPhoneNo}" HeaderStyle="{StaticResource FinalBillsView_DataGridColHeaderStyle}" Header="Cell phone No"/> </Controls:DataGrid.Columns> </Controls:DataGrid> 

In the datatemplate for the datragrid row, I bind the checboxs property IsChecked IsSelected property from the Bill class.

 <Controls:DataGrid.RowHeaderTemplate> <DataTemplate> <Grid> <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:DataGridRow}}}"/> </Grid> </DataTemplate> </Controls:DataGrid.RowHeaderTemplate> 

The problem is that I set the IsSelected property to true for all elements in the list.

  foreach (var row in TmobileBill) { row.IsSelected = true; } 

The flags in the view are not marked. What is the root of the problem?

Thanks.

+4
source share
1 answer
  • Try changing IList<Bill> to ObservableCollection<Bill>
  • Try using a simple binding <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>

For debugging purposes, specify the following item along with the CheckBox control to see what is actually associated with the RowItem:

 <TextBlock Text="{Binding}"></TextBlock> 
0
source

All Articles