PropertyChanged事件未触发

时间:2011-12-23 04:56:26

标签: c# paging wpfdatagrid

我正在使用ObservableCollection<>为DataGrid实现分页。 第一次正确显示10条记录。单击Next按钮时,它没有显示第二页。当我调试时,我发现当我设置currentpagenumber时,PropertyChanged事件没有触发。

我的代码是

   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

我收集了riskCollection中的所有数据。我的DataGrid名称是grdRiskAlerts 我绑定为

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

对于这个传呼内容,我跟随谷歌的一个例子 我不知道为什么propertychanged事件没有解雇。

任何人都可以帮忙吗?

拉​​姆金。

1 个答案:

答案 0 :(得分:4)

明显的问题是您是直接设置值而不是更新绑定源。看看你是否将一些源绑定到一个属性然后手动更改该属性,你打破绑定,这就是为什么属性改变不触发。