6

I have the following ui items - one checkbox list and one checkbox with toggle all checkboxes in that list -

<DockPanel>
    <CheckBox
        Name="SelectCheckboxes"
        Command="{Binding ToggleCheckBoxes}"
        Content="Whatever"/>
</DockPanel>
<DockPanel>
    <ListBox Name="MyListBox"
             ItemsSource="{Binding Path=MyProperty, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="MyCheckBox"
                          Content="{Binding myvalue}"
                          Tag="{Binding mycode}"
                          IsChecked="{Binding Path=isChecked, Mode=TwoWay}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DockPanel>

And here is the MyProperty property -

public ObservableCollection<SomeEntity> MyProperty
{
    get { return _someEntities; }
    set
    {
        if (value == _someEntities)
            return;

        _someEntities = value;
        base.OnPropertyChanged("MyProperty");
    }
}

And here is a command ToggleCheckBoxes -

public ICommand ToggleCheckBoxes
{
    get
    {
        if (_toggleCheckBoxesCommand == null)
        {
            _toggleCheckBoxesCommand = new RelayCommand(
                param => this.toggleCheckBoxes()
                );
        }
        return _toggleCheckBoxesCommand;
    }
}

void toggleCheckBoxes()
{
    foreach (var i in MyProperty)
    {
        if (i.isChecked)
            i.isChecked = false;
        else
            i.isChecked = true;
    }
}

When I click on the checkbox to toggle the checkboxes, I can look at the property in the code and see that the isChecked property is changed, but the ListBox does not update to reflect that all items are checked/unchecked.

Does anyone see anything that I am missing that might cause the ListBox not to update?

Thnaks for any thoughts.

6
  • SomeEntity implements INotifyPropertyChanged, right? Commented Jul 29, 2011 at 13:24
  • @H.B. - I'm betting isChecked is a field, not a property. Commented Jul 29, 2011 at 13:28
  • You'd have to show the code for SomeEntity. Commented Jul 29, 2011 at 13:29
  • @CodeNaked: That'd be even more awful. Commented Jul 29, 2011 at 13:29
  • You are correct - isChecked is a field - not a property. Commented Jul 29, 2011 at 13:31

1 Answer 1

10

Make sure that your isChecked member is actually a property and that SomeEntity implements INotifyPropertyChanged. Something like:

public class SomeEntity : INotifyPropertyChanged {
    private bool _isChecked;
    public bool isChecked
    {
        get { return _isChecked; }
        set
        {
            if (value == _isChecked)
                return;

            _isChecked= value;
            this.NotifyPropertyChanged("isChecked");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was it - I just had INotifyPropertyChanged on the Entity, not on the property within the entity. Thanks.

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.