2

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");
        }
    }
1
  • Your code seems to be wrong. Why do you want to bind it to a viewmodel? Why don't you try to bind the checkbox to the header checkbox isChecked? Commented May 5, 2016 at 8:19

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

The main problem is When I am checking it half of the list (which is present at below part of the scroll bar ) is checked only and then again I scroll it up,the rest of the checkboxes are checked.
I have used updatesourcetrigger=true but still not working
check trigger event OnPropertyChanged("IsSelected"); instead of this give OnPropertyChanged("AllSelected");.. Also give ancestor type into window or usercontrol what ever it may be ...
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.