ankita kumari
ankita kumari

Reputation: 185

Datagrid not updating properly, updates while scrolling

I am implementing a select all check box in datagrid in wpf using 'mvvm' pattern. The problem which I face is on checking the header "Select All" checkbox, all the checkboxes get checked but only after scrolling down the scroll bar. How to fix this issue?

Scrolling down after checking 'select all'

[

And again after scrolling back up

[

 private bool _IsSelected;
    public bool IsSelected
    {
        get { return _IsSelected; }
        set
        {
            _IsSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

    private bool _AllSelected;
    public bool AllSelected
    {
        get { return _AllSelected; }
        set
        {
            _AllSelected = value;
            foreach (var reportListItemModel in UsecaseListItems)
            {
                reportListItemModel.IsSelected = this._AllSelected;
                OnPropertyChanged("IsSelected");
            }
            OnPropertyChanged("AllSelected");

        }
    }


    private ObservableCollection<UseCase> _usecaseListItems = new ObservableCollection<UseCase>();
    public ObservableCollection<UseCase> UsecaseListItems
    {
        get { return _usecaseListItems; }
        set {
            _usecaseListItems = value;
            OnPropertyChanged("UsecaseListItems");
        }
    }

Xaml code

<DataGrid Grid.Row="0" ItemsSource="{Binding Path=UsecaseListItems, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" Name="MyDataGrid"
          CanUserAddRows="False" >
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"  Width="50">
                <DataGridCheckBoxColumn.HeaderTemplate>
                    <DataTemplate x:Name="dtAllChkBx">
                        <CheckBox Name="cbxAll" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=UserControl },Mode=TwoWay}"/>
                    </DataTemplate>
                </DataGridCheckBoxColumn.HeaderTemplate>
            </DataGridCheckBoxColumn>
            <DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding UsecaseName}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Upvotes: 3

Views: 1651

Answers (1)

bdimag
bdimag

Reputation: 963

This is behavior I've observed when the model represented in the DataGrid rows (in this case UseCase) is not implementing INotifyPropertyChanged. They update during scroll because they are redrawn and the new values are read from the properties at that time.

Upvotes: 1

Related Questions