2013-04-14 36 views
3

我試圖在LongListSelector中綁定一些CheckBoxes。它們綁定,當視圖呈現時,正確的CheckBoxes被選中/取消選中,但我無法通過選中/取消選中CheckBox來修改我的底層對象。將CheckBoxes綁定到LongListSelector

<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>  

我的視圖模型中有下面的代碼:

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; } 
} 

我試圖訂閱CollectionChanged事件,但它沒有被被解僱。

我總是可以在代碼隱藏中處理Checked和Unchecked,但我寧願不要,並且處理我的viewmodel中的所有內容。

我非常感謝任何關於如何讓綁定正常工作的信息。

+0

你可以發佈你的PlaceOfInterestCategory類嗎? – Marc

+0

有沒有必要添加標籤到您的標題,有一個標籤系統。請閱讀http://meta.stackexchange.com/q/19190進行討論。同樣,由於右下角的角色卡片,「謝謝」已被「覆蓋」,所以這也不是必需的。 – Patrick

回答

1

使PlaceOfInterestCategory實現INotifyPropertyChange並在屬性設置器中調用OnPropertyChanged()。當您綁定到可觀察集合的項目時,我的視圖是PlaceOfInterestCategory,它們應該執行INPC。您是否嘗試在您的設置器中設置斷點以查看在檢查複選框時是否實際更新了屬性?他們沒有被設置,或者這些更改沒有反映在您的用戶界面中?

+0

非常感謝!我的印象是,ObservableCollection 爲我處理INPC,但只適用於簡單的類型,事後看來非常合理。 – Francis

相關問題