AllCodeFragments are manually input and are not copied from the project. Therefore, they cannot be accurate. They are only used in concert with the ideas described.
In the previous article, we used binding to construct a simple dependencypropertywatcher class, which enables us to hook up a callback function to detect any changes to the dependencyproperty attribute. Next we will use it to expand datapager, make it more in line with the appearance and information we are used.
First, check the controltemplate of the native datapager and find the following definitions:
The stackpanel in the horizontal arrangement is the main presentation container of datapager (mentioned in the previous article), but it does not provide the name attribute, therefore, we can only get the reference from the grid named root, and add a textblock at the end to prepare the text information. The Code is as follows:
1:Public Override VoidOnapplytemplate ()
2:{
3:Base. Onapplytemplate ();
4:
5:Grid grid = (GRID) gettemplatechild ("Root");
6:Border border = (Border) grid. Children [0];
7:Stackpanel Panel = (stackpanel) border. Child;
8:
9:Counttext =NewTextblock ();
10:Counttext. Margin =NewThickness (10, 0, 5, 0 );
11:Counttext. fontsize = 12;
12:Counttext. fontfamily =NewSystem. Windows. Media. fontfamily ("Simsun");
13:Counttext. verticalalignment = verticalalignment. Center;
14:
15:_ Watcher. Attach (This,"Source", Sourcechanged );
16:
17:Panel. Children. Add (counttext );
18:}
Here we focus on implementation extension. textblock settings are provided in hard encoding format.
Next, we need to update the text attribute of counttext in a timely manner when the attributes we are interested in change. In general, we will sort out a pagedcollectionview object as the source of datapager, according to our habits, we need to set the pagedcollectionview's count attribute (currently displayed quantity), itemcount attribute (total quantity), pageindex (current page index), and datapage's pagecount attribute (total number of pages). The most direct method is to use the dependencypropertywatcher class to add a changed callback to each attribute, such as attach (this, "source. Count", countchanged. However, to view the definition of pagedcollectionview, it has implemented the inotifycollectionchanged and inotifypropertychanged interfaces. Therefore, we only need to pay attention to the source changes, then, the events using these two interfaces can respond to all the preceding attribute changes. The pagecount attribute of datapager itself is actually related to the pagesize and itemcount of pagecollectionview and does not need to be processed separately. Therefore, although there are many attributes to be processed, in the above Code, we only use an attach in the 15 rows, and the definition of sourechanged is quite concise:
1:Private VoidSourcechanged (Object Value)
2:{
3:Pagedcollectionview PCV = (pagedcollectionview)Value;
4:
5:If(PCV! =Null)
6:{
7:PCV. propertychanged + =NewSystem. componentmodel. propertychangedeventhandler (pcv_propertychanged );
8:}
9:
10:Updatecountstring ();
11:}
12:
13:VoidPcv_propertychanged (ObjectSender, system. componentmodel. propertychangedeventargs E)
14:{
15:Updatecountstring ();
16:}
When the source attribute of datapager changes, we get a new source value. If it is a pagedcollectionview object, it will process its propertychanged event, if you want to provide detailed details, you can also handle the collectionchanged event, but for our habits, propertychanged is enough to handle. Updatecountstring is very casual and can be processed according to your hobbies. The best thing is to provide a stringformat attribute for our new datapager, but this is not the focus of this article, here I only provide one simple implementation:
1:Private VoidUpdatecountstring ()
2:{
3:Pagedcollectionview PCV = (pagedcollectionview) source;
4:
5:IntPagecount = 1;
6:IntCount = 0;
7:IntItemcount = 0;
8:IntPageindex = 0;
9:
10:If(PCV! =Null)
11:{
12:Pagecount = pagecount;
13:Count = PCV. count;
14:Itemcount = PCV. itemcount;
15:Pageindex = PCV. pageindex;
16:}
17:
18:Counttext. Text =String. Format ("{0} total records {1} page {2} page {3} currently displayed", Itemcount, pagecount, pageindex + 1, count );
19:}
Now, a datapager with text information has been completed. The following two links are attached: