2013-05-03 128 views
0

這是我第一個基於Visual Studio提供的默認模板的第一個Windows8應用程序。問題是當我嘗試通過在LoadData()中創建Observable集合的新實例來分配Items屬性值時,數據沒有綁定,但是當我使用Items.Add方法將項添加到列表中時,我可以在UI中看到數據。我希望有些人可以解釋我的行爲,如果我錯過了任何非常明顯的事情。基於MVVM的Windows Phone 8應用程序綁定中的問題

namespace Sample.ViewModels 
{ 
    public class MainViewModel : INotifyPropertyChanged 
    { 
     public MainViewModel() 
     { 
      this.Items = new ObservableCollection<ItemViewModel>();      
     } 

     /// <summary> 
     /// A collection for ItemViewModel objects. 
     /// </summary> 
     public ObservableCollection<ItemViewModel> Items { get; private set; } 

     private string _sampleProperty = "Sample Runtime Property Value"; 

     /// <summary> 
     /// Sample ViewModel property; this property is used in the view 
     /// to display its value using a Binding 
     /// </summary> 
     /// <returns></returns> 
     public string SampleProperty 
     { 
      get 
      { 
       return _sampleProperty; 
      } 
      set 
      { 
       if (value != _sampleProperty) 
       { 
        _sampleProperty = value; 
        NotifyPropertyChanged("SampleProperty"); 
       } 
      } 
     } 

     /// <summary> 
     /// Sample property that returns a localized string 
     /// </summary> 
     public string LocalizedSampleProperty 
     { 
      get 
      { 
       return AppResources.SampleProperty; 
      } 
     } 

     public bool IsDataLoaded 
     { 
      get; 
      private set; 
     } 

     /// <summary> 
     /// Creates and adds a few ItemViewModel objects into the Items collection. 
     /// </summary> 
     public void LoadData() 
     { 

      try 
      { 
       using (IQContext context = new IQContext("isostore:/Test.sdf")) 
       { 
        var query = (from c in context.Categories 

           select new ItemViewModel 
           { 
            CategoryId = c.CategoryId, 
            CategoryName = c.CategoryName 

           }); 

        // Items present in the list. 
        this.Items = 
         new ObservableCollection<ItemViewModel>(query); 

        // this.Items.Add(new ItemViewModel() 
        //  { CategoryId = 1, CategoryName = "Rishi"}); // This Works 

        this.IsDataLoaded = true; 
       } 
      } 

      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyPropertyChanged(String propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (null != handler) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

回答

1

Items財產應實現INPC因爲你改變LoadData參考和UI需要通知:

public ObservableCollection<ItemViewModel> Items { get; private set; } 

private ObservableCollection<ItemViewModel> items; 
public ObservableCollection<ItemViewModel> Items 
{ 
    get 
    { 
     return this.items; 
    } 

    private set 
    { 
     this.items = value; 
     NotifyPropertyChanged("Items"); 
    } 
} 
+0

感謝..理解。 – Rishikesh 2013-05-03 17:39:00

+1

要澄清:ObservableCollection有一個事件,CollectionChanged,其中一些標準視圖(ListBox,LongListSelector等)訂閱,以便他們可以執行增量更新。當您更改引用時,訂閱偵聽器無法知道這一點 - 因此您必須先NotifyProperyChanged(之後視圖將放棄舊的CollectionChanged偵聽器,併爲新集合添加一個偵聽器。) – pantaloons 2013-05-03 21:00:50

相關問題