0

これは、Visual Studio によって提供される既定のテンプレートに非常に基づいている、私の最初の windows8 アプリケーションです。問題は、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