Home > OS >  How to change the ischecked state of a each element in a list of checkbox?
How to change the ischecked state of a each element in a list of checkbox?

Time:06-09

I'm defining a list of check boxes as follows:

<ListBox Name="listBoxZone" ItemsSource="{Binding Nr5GRRCList}" Background="Azure" Margin="346,93,89,492" Grid.Column="1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem IsSelected="{Binding IsChecked}">
                <CheckBox x:Name="RRC5G_CheckBox" 
                      Content="{Binding messageType}" 
                      IsChecked="{Binding IsChecked}" 
                      Checked="RRC5G_CheckBox_Checked" 
                      Unchecked="RRC5G_CheckBox_Unchecked"
                      Margin="0,5,0,0"/>
            </ListBoxItem>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Where Nr5GRRCList related code is:

public ObservableCollection<BoolStringClass> Nr5GRRCList { get; set; }
public class BoolStringClass
{
    public string messageType { get; set; }
    public bool IsChecked { get; set; }
}

if (Nr5GRRCList == null)
{
    Nr5GRRCList = new ObservableCollection<BoolStringClass>();
}

foreach(string filter in rrc5GFilters)
{
    Nr5GRRCList.Add(new BoolStringClass { messageType = filter, IsChecked = false });
}

This is working fine: enter image description here

I'm trying to add an checkbox to control all checkboxes in this list:

  • I'll still be able to check/uncheck the checkboxes individually
  • I want to be able to check/uncheck the new checkbox and get all the checkboxes checked/unchecked

I tried adding the new checkbox:

<CheckBox x:Name="checkBox_NR5G_RRC" Content="RRC" Checked="HandleCheck_RRC5G" Unchecked="HandleUncheck_RRC5G" Height="Auto" Width="Auto" Margin="334,76,341,607" Grid.Column="1"/>

enter image description here

I can't find a way of modifying the ischecked value of each individual checkbox when the 'RRC' is checked/unchecked. All I seem to have access to is a list of BoolStringClass elements.

Any tipd would be much appreciated. Thanks!

CodePudding user response:

All I seem to have access to is a list of BoolStringClass elements

that is all data you need.

foreach(BoolStringClass item in Nr5GRRCList)
{
    item.IsChecked = !item.IsChecked;
    // item.IsChecked = true;
    // item.IsChecked = false;
}

to see update in UI you need to add notifications in BoolStringClass, e.g by implementing INotifyPropertyChanged:

public class BoolStringClass: INotifyPropertyChanged
{
    public string messageType { get; set; }

    private bool _IsChecked;
    public bool IsChecked 
    {
        get { return _IsChecked; }
        set
        {
            _IsChecked = value;
            OnPropertyChanged(nameof(IsChecked));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    } 
}
  • Related