0

I have a Boolean list that is bound to, displaying a list of checkboxes.

I now want to update a Label showing how many of the checkboxes are checked. This must update whenever any checkbox is checked.

I'm struggling as I don't know what to bind the checkbox to, so that I can update the string the label is bound to.

To do this, I think I need to bind each checkbox to a Command, which is working. However, I can't work out how to update the label. If there's a simpler way to do this (I'm sure there is) please let me know.

public class CheckBoxBoolean : INotifyPropertyChanged
{
    public bool CheckBoxChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;

            OnPropertyChanged("CheckBoxChecked");
        }
    }

    public CheckBoxBoolean(bool isChecked)
    {
        _isChecked = isChecked;
    }

    private bool _isChecked = false;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

<StackPanel Grid.Row="0" Grid.Column="1">
    <ItemsControl ItemsSource="{Binding Path=Patterns1}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=CheckBoxChecked, Mode=TwoWay}" Command="{Binding Path=DataContext.CheckBoxChanged, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

    private ICommand _checkBoxChanged;

    public string Percentage1 { get; set; }

    public ICommand CheckBoxChanged
    {
        get
        {
            if(_checkBoxChanged == null)
            {
                _checkBoxChanged = new CheckBoxChanging(Patterns1);
            }
            return _checkBoxChanged;
        }
    }

    public class CheckBoxChanging : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private readonly ObservableCollection<CheckBoxBoolean> _Patterns;

        public CheckBoxChanging(ObservableCollection<CheckBoxBoolean> items)
        {
            _Patterns = items;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            int i = 0;
            foreach (CheckBoxBoolean chk in _Patterns)
            {
                if (chk.CheckBoxChecked)
                    i++;
            }
            Debug.WriteLine("UPD - Pattern 1 % = " + i / 16d);
        }
    }

Alex Chard
  • 41
  • 1
  • 7

1 Answers1

1

Hi I just wrote an example for ur scenario take a look I think it will be helpful

Xaml

  <StackPanel Grid.Row="0" Grid.Column="1">
        <ItemsControl ItemsSource="{Binding Path=CBItems}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=CheckBoxChecked, Mode=TwoWay}" Command="{Binding Path=DataContext.CheckBoxChanged, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Label Content="{Binding LabelContent}"></Label>
    </StackPanel>

Ctro (setting datacontext)

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MainWindowVM DC = new MainWindowVM();
            DC.Init();
            this.DataContext = DC;
        }
    }

Relay command and Model Class

 class MainWindowVM : INotifyPropertyChanged
    {
        public MainWindowVM()
        {

            CheckBoxChanged = new RelayCommand(CheckBoxChangedMethod);

        }

        private string labelContent="Not Yet Checked";

        public string LabelContent
        {
            get { return labelContent; }
            set { labelContent = value; OnPropertyChanged(new PropertyChangedEventArgs("LabelContent")); }
        }


        public void Init()
        {
            try
            {
                CBItems = new ObservableCollection<ex>();
                for (int i = 985; i <= 1030; i++)
                    CBItems.Add(new ex { CheckBoxChecked = true });
            }
            catch (Exception ex)
            {

            }
        }

        public ICommand CheckBoxChanged { get; set; }



        private ObservableCollection<ex> _CBItems;
        public ObservableCollection<ex> CBItems
        {
            get { return _CBItems; }
            set
            {
                _CBItems = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CBItems"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }


        public void CheckBoxChangedMethod(object obj)
        {
            LabelContent = "You have Clicked the checkbox";
        }

    }

    public class RelayCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;

            }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }


        public bool CanExecute(object parameter)
        {
            //return this.canExecute == null || this.canExecute(parameter);
            return true;
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }

        public RelayCommand(Action<object> execute)
        {
            this.execute = execute;

        }


    }

    public class ex
    {
        private bool _checkBoxChecked;

        public bool CheckBoxChecked
        {
            get { return _checkBoxChecked; }
            set { _checkBoxChecked = value; }
        }

    }
Avinash Reddy
  • 937
  • 8
  • 22