# Windows Community Toolkit 4.0-datagrid-part02

Source: Internet
Author: User
Tags abs

Overview

In the previous Windows Community Toolkit 4.0-datagrid-part01, we did a detailed share of the CollectionView section of the DataGrid control. In this article, we will do a detailed sharing of the classes in the Utilities folder.

The following is an example and Code/doc address for the Windows Community Toolkit sample App:

Windows Community Toolkit Doc-datagrid

Windows Community Toolkit Source Code-datagrid

Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls.DataGrid;

Development process

First, let's look at the code structure of the Utilities folder:

You can see that the classes in the Utilities folder are primarily basic and helper classes, so let's look at some important class code:

1. Doubleutil

The function of the class is to determine whether the value of the two double type is close, the size relationship, etc., this method uses a Areclose (v1, V2) method, this method mainly determines whether two values are similar, the calculation method is, when two values of the difference, divided by two values of absolute value and plus 10.0 When the value is less than double epsilon, the two values are considered to be close. A double epsilon represents the smallest double value greater than 0.

`Internal const Double Dbl_epsilon = 1.1102230246251567e-016;Public Static BOOLAreclose (DoubleValue1,Doublevalue2) {    //In case they is infinities (then epsilon check does not work)    if(value1 = =value2) {        return true; }    //This computes (|value1-value2|/(|value1| + |value2| + 10.0)) < Dbl_epsilon    DoubleEPS = (math.abs (value1) + math.abs (value2) +10.0) *Dbl_epsilon; DoubleDelta = value1-value2; return-eps < Delta && EPS >Delta;}`

2. Extensions

The function of this class is the extension of the DataGrid control, which has the following extension methods:

• ishandlersuspended-Identification of the processor suspension;
• Containschild-traverse the visual tree to determine whether the current control contains a child element, which is reflected in many of the WPF UWP controls;
• Containsfocusedelement-traverse the visual tree to determine whether the current control contains the element that has the focus;
• Getitemtype-Gets the element type, divides into the enumeration and the set two sorts to judge;
• Setstylewithtype-Sets the style of the element;
• Setvaluenocallback-Sets the value and interrupts the callback;
• Translate-Calculates the movement of coordinates between the starting and terminating elements;
• Ensuremeasured-When the control is placed in the background layer, the dimension needs to be calculated;
• Suspendhandler-processing of the suspend processor;

3. indextovaluetable

The functionality of this class is the processing between the index of the DataGrid control and the value table, and we look at several important methods:

1) Containsall ()

The function of this method is to determine whether the index range between the given StartIndex and EndIndex is all contained in the table, and the process is mainly based on the values of StartIndex and EndIndex and the Upperbound and L of each range in the list. The value of Owerbound to determine if StartIndex and EndIndex are included in a range;

` Public BOOLContainsall (intStartIndex,intEndIndex) {    intStart =-1; intEnd =-1; foreach(Range<t> Rangeinch_list) {        if(Start = =-1&& range. Upperbound >=StartIndex) {            if(StartIndex <range. Lowerbound) {return false; } Start=StartIndex; End=range.            Upperbound; if(End >=EndIndex) {                return true; }        }        Else if(Start! =-1)        {            if(Range. Lowerbound > End +1)            {                return false; } End=range.            Upperbound; if(End >=EndIndex) {                return true; }        }    }    return false;}`

2) Findrangeindex ()

Gets the index of the current range, which is calculated by traversing the range collection in the list, calculating front and end, and getting the middle value each time, if the upper and lower bounds of range contains the index, returns the index, otherwise, calculates the value based on the comparison of front and end. returned as an index;

`Private intFindrangeindex (intindex) {    if(_list. Count = =0)    {        return-1; }    intFront =0; intEnd = _list. Count-1; Range<T> range =NULL;  while(End >front) {        intMedian = (front + end)/2; Range=_list[median]; if(Range. Upperbound <index) {Front= median +1; }        Else if(Range. Lowerbound >index) {End= Median-1; }        Else        {            returnmedian; }    }    if(Front = =end) {Range=_list[front]; if(Range. Containsindex (index) | | (Range. Upperbound <index)) {            returnFront; }        Else        {            returnFront-1; }    }    Else    {        returnend; }}`

4. Typehelper

The class is the type helper class for the DataGrid control, and the main function is to obtain information such as type, property information, display name, and so on. These methods are more common methods, if you are interested, you can go to the detailed view of the code, here do not repeat.

5. Validationutil

The main function of this class is the checksum of the DataGrid control, let's take a look at the Findequalvalidationresult method:

` Public StaticValidationresult Findequalvalidationresult ( ThisIcollection<validationresult>collection, Validationresult target) {    foreach(Validationresult Oldvalidationresultinchcollection) {        if(Oldvalidationresult.errormessage = =Target. errormessage) {BOOLMovedold =true; BOOLMovedtarget =true; IEnumerator<string> oldenumerator =OldValidationResult.MemberNames.GetEnumerator (); IEnumerator<string> targetenumerator =Target.            Membernames.getenumerator ();  while(Movedold &&movedtarget) {Movedold=Oldenumerator.movenext (); Movedtarget=Targetenumerator.movenext (); if(!movedold &&!)movedtarget) {                    returnOldvalidationresult; }                if(movedold! = Movedtarget | | oldenumerator.current! =targetenumerator.current) { Break; }            }        }    }    return NULL;}`

6. VisualStates

This class is the visual state class for the DataGrid control, and it mainly classifies several state groups:

• Common-normal,pointerover,pressed,disabled
• Expanded-expanded,collapsed,empty
• Focus-unfocused,focused
• selection-selected,unselected
• Active-active,inactive
• Current-regular,current,currentwithfocus
• Interaction-display,editing
• Sort-unsorted,sortascending,sortdescending
• Validation-invalid,rowinvalid,rowvalid,valid
• Scrollbars-touchindicator,mouseindicator,mouseindicatorfull,noindicator

Summarize

Here we have completed the Utilities related class of the DataGrid, and as a second section of the DataGrid-related share, we will continue to share the key points of the most important DataGrid.

Finally, with everyone Amway Windowscommunitytoolkit official micro-blog:https://weibo.com/u/6506046490, You can follow the latest news through Weibo.

Heartfelt thanks to Windowscommunitytoolkit for the outstanding work of the authors, thanks to each contributor, Thank much, all Windowscommunitytoolkit AUTHORS!!!

Windows Community Toolkit 4.0-datagrid-part02

Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD