ankita kumari
ankita kumari

Reputation: 185

select all checkbox in datagrid header not working properly in wpf mvvm

This is what i wantI am working on wpf mvvm datagrid and trying to bind the select all checkbox with the view model. It is not giving me proper result. I am giving my code details here(The xaml code and the view model code)

 <DataGrid Grid.Row="0" ItemsSource="{Binding Path=UsecaseListItems}" AutoGenerateColumns="False" Name="MyDataGrid"
          CanUserAddRows="False" >
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding IsSelected}" Width="50" >
                <DataGridCheckBoxColumn.HeaderTemplate>
                    <DataTemplate x:Name="dtAllChkBx">
                        <CheckBox Name="cbxAll" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid},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>


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");

        }
    }


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

Upvotes: 2

Views: 1303

Answers (3)

Mark
Mark

Reputation: 93

Same problem, I had...

RelativeSource={RelativeSource AncestorType=DataGrid}

It didn't work, so I tried...

RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}

... and it worked.

Upvotes: 1

karthik krishnasamy
karthik krishnasamy

Reputation: 46

Check trigger event OnPropertyChanged("IsSelected");. Instead of using this, give OnPropertyChanged("AllSelected");. Also give ancestor type into window or user-control what ever it may be ...

Upvotes: 0

karthik krishnasamy
karthik krishnasamy

Reputation: 46

Since allselected property in view model , you have to give ancestortype into window or user control instead of datagrid...

Upvotes: 0

Related Questions