5
votes

I use Caliburn Micro as MVVM framework in WPF app. I have little problem how select all checkbox in datagrid control. Every datagrid row have checkbox.

I bind on datagrid property type of 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 datatemplate for datragrid row I bind on checbox’s property IsChecked property IsSelected from class Bill.

<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>

Problem is if I set property IsSelected on true for all items in list.

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

Checkboxes in View are not checked. What is a root of problem?

Thank you.

1
What type of control are you using? (Datagrid)123 456 789 0

1 Answers

1
votes
  1. Try to change IList<Bill> to ObservableCollection<Bill>
  2. Try to use simple binding <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>

For debug purposes define along with CheckBox next control to see what actually binds to RowItem:

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