3

I'm trying to bind some CheckBoxes in a LongListSelector. They bind, and the correct CheckBoxes are checked/unchecked when the view is rendered, but I am unable to modify my underlying object by checking/unchecking the CheckBoxes.

<Grid Grid.Row="3">
<phone:LongListSelector ItemsSource="{Binding PlaceOfInterestCategories}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Name}"/>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>    

My ViewModel has the following code in it:

private ObservableCollection<PlaceOfInterestCategory> _placeOfInterestCategories;

public ObservableCollection<PlaceOfInterestCategory> PlaceOfInterestCategories
{
    get { return _placeOfInterestCategories; }
    set
    {
        if (_placeOfInterestCategories != value)
        {
            _placeOfInterestCategories = value;

            OnPropertyChanged();
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

-

[DataContract]
public class PlaceOfInterestCategory
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public bool IsSelected { get; set; }
}

I've tried to subscribe to the CollectionChanged event, but it's not being fired.

I could always handle Checked and Unchecked in my codebehind, but I'd rather not, and handle everything in my viewmodel.

I'd greatly appreciate any input as to how I can get the binding working properly.

Francis
  • 1,214
  • 12
  • 19
  • Can you post your PlaceOfInterestCategory class? – Marc Apr 14 '13 at 12:48
  • There's no need to add tags to your title, there's a tag system for that. Please read http://meta.stackexchange.com/q/19190 for the discussion. Likewise the "Thanks" are already "covered" due to your character card in the bottom right, so that's not needed either. – Patrick Apr 14 '13 at 13:22

1 Answers1

1

Make PlaceOfInterestCategory implement INotifyPropertyChange and call OnPropertyChanged() in the properties setters. As you are binding to the observable collection's items i your view, which are PlaceOfInterestCategory, they should implement INPC. Have you tried setting a breakpoint in your setters to see whether the properties are actually updated when you check your checkboxes? Are they not being set or are the changes not reflected in your UI?

Marc
  • 12,706
  • 7
  • 61
  • 97
  • Thanks a lot! I was under the impression that ObservableCollection handled INPC for me, but that only applies for simple types, which in hindsight makes perfect sense. – Francis Apr 15 '13 at 19:44