开发者

Select all checkboxes in datagrid rows from view model

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>

        <C开发者_StackOverflow中文版ontrols: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. 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜