I have a DataPager, and once it is populated with pages, it throws a nullreferenceexception. It was kinda weird because back when I have little or no data, it doesn’t throw anything. My code is here:
private void dataPager_PageIndexChanged(object sender, System.EventArgs e)
{
if ((sender as DataPager).Visibility == System.Windows.Visibility.Visible)
{
if ((sender as DataPager).PageIndex == (sender as DataPager).PageCount - 1)
{
(sender as DataPager).GetVisualDescendants().OfType<Button>().Where(b => b.Name == "NextPageButton").SingleOrDefault().IsEnabled = false;
}
else
(sender as DataPager).GetVisualDescendants().OfType<Button>().Where(b => b.Name == "NextPageButton").SingleOrDefault().IsEnabled = true;
}
}
What I don’t get is why does result view of (sender as DataPager).GetVisualDescendants() yield no result when dataPager gets to 4 pages? As you can see, I have already checked if it was Visible for getting visual descendants, and this code is on pageindexchanged event, so it fires as soon as the datapager is loaded. Any thoughts on this one? Any help would be much appreciated. Thanks!
The answer was it fires the pageindexchanged twice. Dunno why, but Silverlight seems to fire pageindexchanged when datapager is loading AND loaded. So it fires this code twice, all I did was added a line that checks if the GetVisualDescendants().Count != 0 and everything went well, atleast, I hope.