简体   繁体   中英

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

I can see the following block need to be changed, Please share full xaml file so I can understand your data binding.

<CheckBox  IsChecked="{Binding IsSelected,Mode=TwoWay}" Command="{Binding CheckSingle}"/>

You need to edit it as "DataContext.IsSelected" and need to set binding source, it may be data grid or even full xaml based on your business logic to bind data. An example:

<CheckBox  IsChecked="{Binding DataContext.IsSelected,Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Command="{Binding CheckSingle}"/>

I've just given sample example, there are many other ways to achive it.

Maybe I am saying shit but I would try that:

  • Remove all commands
  • in isChecked set I would put all you put in selectAllButtonExecute
  • in isChecked get I would check if all checkbox are checked and return true or false accordingly
  • in IsSelected set I would add OnPropertyChanged("IsChecked");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM