Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How To impliment Select All/Deslect All functionality in WPF datagrid in MVVM.

$
0
0

HI All,

I am having a datagrid with SelectAll checkbox in column header of checkbox column.

XAML:

------------

     <DataGrid.Columns>
                            <DataGridTemplateColumn Width="30">
                                <DataGridTemplateColumn.Header>
                                    <CheckBox Name="CheckSelectAll"
                                              IsChecked="{Binding DataContext.IsSelectAll,Mode=TwoWay, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}" >
                                      </CheckBox>
                                    
                                    </DataGridTemplateColumn.Header>
                                        <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="{Binding Path=IsSelected,Mode=Twoway,UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                          

Then I am having Viwmodel With SelectAll Property:


        public bool? IsSelectAll
        {
            get
            {
                return _isSelectAll;
            }

            set
            {
                _isSelectAll = value;
                if (_isSelectAll.HasValue && _isSelectAll.Value)
                {
                    SelectAll();
                }
                else
                {
                    DeselectAll();
                }
                OnPropertyChanged("IsSelectAll");
            }
        }

-----

   private void SelectAll()
        {
            foreach (var   InfoViewModel in Results)
            {
               InfoViewModel.IsSelectedInvoice = true;
            } 
        }
        
        private void DeselectAll()
        {
            foreach (var  InfoViewModel in Results)
            {
                 nfoViewModel.IsSelectedInvoice = false;
            }  
        }

---------------------------

So Now my select all and deselect all functionality is working fine .But when i am checking any checkbox manually in grid I want to uncheck Selectall header checkbox.Please guide me to do that.

If i am trying to trigger in set section of IsSelectedInvoice  property then it is circular for SelectAll and not working.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>