0

这是我的第一个 windows8 应用程序,它非常基于 Visual Studio 提供的默认模板。问题是当我尝试通过在 LoadData() 中创建 Observable 集合的新实例来分配 Items 属性值时,数据未绑定,但是当我使用 Items.Add 方法将项目添加到列表中时,我可以在 UI 中看到数据. 我希望有人可以向我解释这种行为以及我是否遗漏了任何非常明显的东西。

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

1 回答 1

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");
    }
}
于 2013-05-03T17:29:02.560 回答