vendredi 22 janvier 2021

Checkbox binding WPF mvvm pattern

I have to bind the changes of checkbox in the listview.

checkbox should work in such a way that if i select all checkbox and uncheck a single checkbox then header checkbox should deselect. Same way if i have 3 child checkbox ,and individually select all then parent checkbox should select.

i am using MVVM pattern

xaml

  <GridViewColumn Width="80"  >
                        <GridViewColumn.Header >
                            <!--<CheckBox  x:Name="cbSelectAll" IsChecked="{Binding ModelDetails.IsChecked}"  Command="{Binding cbSelectAll_Checked}"/>-->
                            <CheckBox  x:Name="cbSelectAll" Command="{Binding CbSelectAll_Checked}"  IsChecked="{Binding IsChecked,   UpdateSourceTrigger=PropertyChanged}"  />
                        </GridViewColumn.Header>

                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <WrapPanel x:Name="Layout"   >
                                        
                                        <CheckBox  IsChecked="{Binding IsSelected,Mode=TwoWay}" Command="{Binding CheckSingle}"/>

                                    </WrapPanel>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

viewmodel.cs

 public class Item : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                //publishing the event in current classs
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public Item(string name, string matches, string date, bool isSelected)
        {
            Name = name;
            Type = matches;
            Date = date;
            IsSelected = isSelected;

        }
        private bool _isSelected;

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

 
        public string Name { get; set; }
        public string Type { get; set; }
        public string Date { get; set; }

    }

here is the another class

public class AddedFilesViewModel : INotifyPropertyChanged
    {

        public ICommand CbSelectAll_Checked { get; set; }
        public ICommand UploadBtnClick { get; set; }
        public ICommand CheckSingle { get; set; }

            CbSelectAll_Checked = new RelayCommands(SelectAllBtnExecute, SelectAllBtnCanExecute);
            UploadBtnClick = new RelayCommands(UploadBtnExecute, UploadBtnCanExecuteUpload);
            CheckSingle = new RelayCommands(SingleCheckBoxExecute, SingleCheckBoxExecuteCanExecute);

  public void SelectAllBtnExecute(object param)
        {
            if (selectedStatus == false)
            {
                foreach (var item in Items)
                {
                    item.IsSelected = true;

                }
                selectedStatus = true;
            }
            else
            {
                foreach (var item in Items)
                {
                    item.IsSelected = false;

                }

                selectedStatus = false;
            }

        }
        public void SingleCheckBoxExecute(object param)
        {
            MessageBox.Show("hello");
        }

        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
              



          OnPropertyChanged("IsChecked");
            
            }
        }


        public bool SelectAllBtnCanExecute(object param)
        {
            return true;
        }

        public void UploadBtnExecute(object param)
        {
            List<Item> selectedFiles = new List<Item>();
            if (Items != null)
            {
                foreach (var selectedItems in Items)
                {
                    if (selectedItems.IsSelected)
                    {
                        selectedFiles.Add(selectedItems);
                    }
      }



Aucun commentaire:

Enregistrer un commentaire