0

I have a CheckBox in a ListBox. I set the ListBox ItemsSource to a List of Agency. Agency has a property

public class Agency 
{ 
  public bool isSelected { get; set;} 
}
<ListBox> <!-- ItemsSource set in codebehind to List<Agency> -->
    <CheckBox IsChecked="{Binding Path=isSelected, Mode=TwoWay}" />
</ListBox>

I have a function to check all the checkboxes

//SelectAll button

    private void SelectAll_Click(object sender, RoutedEventArgs e)
    {
      List<Agency> list = this.AgencySubListBox.ItemsSource as List<Agency>;
      for (int i = 0; i < list.Count; i++)
      {
        Agency d = list[i];
        d.isSelected = true;
      }
    }

When I hit the select all button I expect the checkboxes to all be checked. But nothing happens.

3 Answers 3

1

You have to implement INotifyPropertyChanged for your Agency-class. Then in your isSelected-Property, call PropertyChanged if the value of the property has been changed. Auto-properties as you used in your example do not support INotifiyPropertyChanged, therefore you can not use them for your purpose. If you work with .net, I would also recommend to start the property names with upper case. This is the widely accepted standard.

Sign up to request clarification or add additional context in comments.

Comments

1

You should make your model implement INotifyPropertyChanged, and implement it

Comments

1

There is nothing to tell the UI that the your checkbox binding has been invalidated, and needs rechecking. Therefore, implement INotifyPropertyChanged on your Agency type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.