简体   繁体   中英

PropertyChanged event is not firing

I am implementing paging for DataGrid using ObservableCollection<> . First time it correctly displays 10 records. When click on the Next button, it didn't show the second page. When I debug, I found that PropertyChanged event is not firing when I set currentpagenumber.

My code is

   ObservableCollection<RiskSettings> riskCollection = new ObservableCollection<RiskSettings>();
  private ObservableCollection<ObservableCollection<RiskSettings>> Pages;
    private ObservableCollection<RiskSettings> _ItemsSource;
    private ObservableCollection<RiskSettings> _CurrentPage;
  public RiskAlert()
    {
        InitializeComponent();
        GeneratePages();
    }
   private void GeneratePages()
    {
        if (riskCollection.Count > 0)
        {
            PageCount = (int)Math.Ceiling(riskCollection.Count / (double)ItemsPerPage);
            Pages = new ObservableCollection<ObservableCollection<RiskSettings>>();
            for (int i = 0; i < PageCount; i++)
            {
                ObservableCollection<RiskSettings> page = new ObservableCollection<RiskSettings>();
                for (int j = 0; j < ItemsPerPage; j++)
                {
                    if (i * ItemsPerPage + j > riskCollection.Count - 1) break;
                    page.Add(riskCollection[i * ItemsPerPage + j]);
                }
                Pages.Add(page);
            }
            this.CurrentPage = Pages[0];
            this.CurrentPageNumber = 1;
        }
    }

  public int CurrentPageNumber
    {
        get { return _CurrentPageNumber; }
        set
        {
            _CurrentPageNumber = value;
            //if (PropertyChanged != null)
                this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPageNumber"));
        }
    }
 public ObservableCollection<RiskSettings> Collection
    {
        get
        {
            return riskCollection;
        }
        set
        {
            riskCollection = value;
            GeneratePages();
        }
    }
 public ObservableCollection<RiskSettings> CurrentPage
    {
        get { return _CurrentPage; }
        set
        {
            _CurrentPage = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("CurrentPage"));
        }
    }

    public int CurrentPageNumber
    {
        get { return _CurrentPageNumber; }
        set
        {
            _CurrentPageNumber = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("CurrentPageNumber"));
        }
    }

    public int ItemsPerPage
    {
        get { return (int)GetValue(ItemsPerPageProperty); }
        set { SetValue(ItemsPerPageProperty, value); }
    }
    public static readonly DependencyProperty ItemsPerPageProperty = DependencyProperty.Register("ItemsPerPage", typeof(int), typeof(RiskAlert), new UIPropertyMetadata(20));

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

I collected all data in riskCollection.My DataGrid Name is grdRiskAlerts I binded as

    GeneratePages();
            this.ItemsSource = riskCollection;
            grdRiskAlerts.ItemsSource = CurrentPage;
            txtTotalRecs.Text = "Total : " + riskCollection.Count();
            //txtDispRecs.Text = CurrentPage.Count().ToString();
            txtNumOfPages.Text = this.CurrentPageNumber.ToString();
            totalRecords = riskCollection.Count();
            if (pageSize <= totalRecords)
            {
                if (totalRecords > 0)
                    txtDispRecs.Text = "Displaying 1 to " + CurrentPage.Count();
                else
                    txtDispRecs.Text = "Displaying 0 to " + CurrentPage.Count();
            }

For this paging stuff i followed one example from google I do not know why propertychanged event is not firing.

Can any one help on this?

Ramki.

The clear problem is you are setting the value directly rather than updating the binding source. Look if you bind some source to a property then change that property manually, youy break the binding and thats why property change in not firing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM