Domanda

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.

È stato utile?

Soluzione

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?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top