0

I am working on 'Select All' checkbox in datagrid header in wpf using mvvm pattern. On clicking the checkbox, all the checkboxes gets checked and on uncheking it, the reverse happens. But I am unable to fetch and bind the selected items to the View Model.

My code is like this

 <DataGrid Grid.Row="0" Background="LightGray" CanUserAddRows="False"  AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGridCustomers" ItemsSource="{Binding Path=UsecaseListItems}" CanUserResizeRows="False">
        <DataGrid.Columns>
             <DataGridTemplateColumn >
                <DataGridTemplateColumn.Header>
                    <CheckBox x:Name="headerCheckBox" IsChecked="{Binding Path=MainWindowViewModel.AllSelected, Mode=TwoWay, 
              UpdateSourceTrigger=PropertyChanged}" Command="{Binding DoStuffCommand}" CommandParameter="{Binding ElementName=UserCaseListControl}"/>
                </DataGridTemplateColumn.Header>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="chkSelectAll" HorizontalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Binding="{Binding Path=UsecaseName}" Header="UsecaseName" IsReadOnly="True" >
                <DataGridColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                    <Setter Property="FontWeight" Value="Bold"/>
                    </Style>
                </DataGridColumn.HeaderStyle>
            </DataGridTextColumn>

ViewModel is like:

    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");
        }
    }
Muds
  • 4,006
  • 5
  • 31
  • 53
ankita kumari
  • 185
  • 1
  • 3
  • 16
  • where is the code to bind selected items ? what you do exactly want here ? – Muds May 06 '16 at 07:48
  • I want the slected checkbox values to be stored in a IEnumerable of strings in the view model – ankita kumari May 06 '16 at 07:55
  • UsecaseListItems.Where(p=>p.IsSelected); --- is it something more than this ? – Muds May 06 '16 at 07:56
  • duplicate to http://stackoverflow.com/questions/36999708/how-to-get-the-value-from-a-template-column-in-a-wpf-datagrid/37000291#37000291 and http://stackoverflow.com/questions/37029133/select-all-checkbox-in-header-of-datagrid-and-its-binding-in-wpf-mvvm?lq=1 – Frerk Morrin May 06 '16 at 08:44

1 Answers1

0

With your code, there will be no direct binding of selected items. As all your checkboxes were bind to headerCheckBox.IsChecked.

You could bind to your UsecaseListItem.IsSelected instead, and find another way to to execute selectAll:

e.g. Define the UseCase that inherit ObservableObject and use your above code in AllSelected to change each item's IsSelected property.

Or use a Command (for example) in your headerCheckBox.

Community
  • 1
  • 1
Bolu
  • 8,696
  • 4
  • 38
  • 70