Binding Pivot control with Observable Collection MVVM (windows phone 8)

前端 未结 3 555
北海茫月
北海茫月 2021-01-19 17:22

I\'m new to WP8 & MVVM. I created wp8 app which requests various bits of data once a user has logged in. I just can\'t get my pivots header to get created dynamically an

3条回答
  •  囚心锁ツ
    2021-01-19 18:20

    No you answer is wrong and your code is wrong.

    Error 1:

        set
        {
            if (_pivots != value) this.SetProperty(ref this._pivots, value);
        }
    

    in here it dos not matter if you change the property or the variable the binding will be lost.

    Error 2: All UIElements derived from ItemsControl ignore the INotifyPropertyChanged because it does not update the ItemsSource just the DataContext.

    Working Example

        public ObservableCollection LstLog { get; set; }
        private ObservableCollection _lstContent = new ObservableCollection();
        public ObservableCollection LstContent
        {
            get
            {
                LstLog.Add("get");
                return _lstContent;
            }
            set
            {
                LstLog.Add("set");
                _lstContent = value;
            }
        }
        public MainWindow()
        {
            LstLog = new ObservableCollection();
    
            InitializeComponent();
            this.DataContext = this;
        }
    
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            LstContent.Add("Value added");
        }
    
        private void New_Click(object sender, RoutedEventArgs e)
        {
            _lstContent = new ObservableCollection();
        }
    
        private void NewBind_Click(object sender, RoutedEventArgs e)
        {
            _lstContent = new ObservableCollection();
            listObj.ItemsSource = _lstContent;
        }
    
        private void NewProp_Click(object sender, RoutedEventArgs e)
        {
            LstContent = new ObservableCollection();
        }
    
        private void NewPropBind_Click(object sender, RoutedEventArgs e)
        {
            LstContent = new ObservableCollection();
            listObj.ItemsSource = LstContent;
        }
    

    and the ui

    
        
            
            
            
        
    
        
            
                
                    
                
            
        
        
            
                
                    
                
            
        
        
            

    the LstLog is just to see the event list, if you click add it will update the two list but if click the New or the New Prop the binding is lost until you update it like in the New Bind or New Prop Bind

    Hope this will clarify the XAML List event recurrent problem.

    PS: this is in WPF but workes the same in WP8 and windows store app.

提交回复
热议问题