Windows Community Toolkit 4.0 - DataGrid - Part02
概述
在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part01 中,我们针对 DataGrid 控件的 CollectionView 部分做了详细分享。而在本篇,我们会对 Utilities 文件夹中的类做详细的分享。
下面是 Windows Community Toolkit Sample App 的示例截图和 code/doc 地址:

Windows Community Toolkit Doc - DataGrid
Windows Community Toolkit Source Code - DataGrid
Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls.DataGrid;
开发过程
首先再来看一下 Utilities 文件夹的代码结构:

可以看到 Utilities 文件夹中的类主要是一些基础和帮助类,下面我们来看一些重要的类代码:
1. DoubleUtil
该类的功能主要是判断两个 double 类型的值之间是否接近,大小关系等;这些方法中用到了一个 AreClose(v1, v2) 的方法,这个方法主要判断两个数值是否相近,计算方法是,当两个值的差,除以两个值的绝对值和加10.0 的值小于 double epsilon 时,认为两个数值是接近的。而 double epsilon 表示大于零的最小 double 数值。
internal const double DBL_EPSILON = 1.1102230246251567e-016;
public static bool AreClose(double value1, double value2)
{
// in case they are Infinities (then epsilon check does not work)
if (value1 == value2)
{
return true;
}
// This computes (|value1-value2| / (|value1| + |value2| + 10.0)) < DBL_EPSILON
double eps = (Math.Abs(value1) + Math.Abs(value2) + 10.0) * DBL_EPSILON;
double delta = value1 - value2;
return -eps < delta && eps > delta;
}
2. Extensions
该类的功能是 DataGrid 控件的扩展,主要有以下扩展方法:
- IsHandlerSuspended - 处理器暂停的标识;
- ContainsChild - 遍历可视化树,判断当前控件是否包含某个 child 元素,该方法在 WPF UWP 的很多控件中都有过体现;
- ContainsFocusedElement - 遍历可视化树,判断当前控件是否包含获得焦点的元素;
- GetIsReadOnly - 获取控件的只读属性;
- GetItemType - 获取元素类型,分为枚举和集合两种分类来判断;
- SetStyleWithType - 设置元素的样式;
- SetValueNoCallback - 设置值并中断回调;
- Translate - 计算起始和终止元素间的坐标移动;
- EnsureMeasured - 在控件被置于背景层时,需要计算尺寸;
- SuspendHandler - 暂停处理器的处理;
3. IndexToValueTable
该类的功能是 DataGrid 控件的索引和值表之间的处理,我们看几个重要的方法:
1) ContainsAll()
该方法的作用是判断给定的 startIndex 和 endIndex 间的索引范围,是否全部包含在表中;判断过程主要是根据 startIndex 和 endIndex 的值,以及 list 中的每个 range 的 UpperBound 和 LowerBound 的值,判断 startIndex 和 endIndex 是否包含在某个 range 中;
public bool ContainsAll(int startIndex, int endIndex)
{
;
;
foreach (Range<T> range in _list)
{
&& range.UpperBound >= startIndex)
{
if (startIndex < range.LowerBound)
{
return false;
}
start = startIndex;
end = range.UpperBound;
if (end >= endIndex)
{
return true;
}
}
)
{
)
{
return false;
}
end = range.UpperBound;
if (end >= endIndex)
{
return true;
}
}
}
return false;
}
2) FindRangeIndex()
获取当前 range 的索引,计算过程是:遍历 list 中的 range 集合,计算 front 和 end,每次获取中间值,如果 range 的上下限包含该 index,返回该 index;否则,根据 front 和 end 的对比,计算值作为索引返回;
private int FindRangeIndex(int index)
{
)
{
;
}
;
;
Range<T> range = null;
while (end > front)
{
;
range = _list[median];
if (range.UpperBound < index)
{
front = median + ;
}
else if (range.LowerBound > index)
{
end = median - ;
}
else
{
return median;
}
}
if (front == end)
{
range = _list[front];
if (range.ContainsIndex(index) || (range.UpperBound < index))
{
return front;
}
else
{
;
}
}
else
{
return end;
}
}
4. TypeHelper
该类是 DataGrid 控件的类型帮助类,主要功能是获取类型,属性信息,显示名等信息。这些方法都是较通用的方法,大家如果有兴趣,可以去详细查看代码,这里不做赘述。
5. ValidationUtil
该类的主要功能是 dataGrid 控件的校验,我们来看看 FindEqualValidationResult 方法:
public static ValidationResult FindEqualValidationResult(this ICollection<ValidationResult> collection, ValidationResult target)
{
foreach (ValidationResult oldValidationResult in collection)
{
if (oldValidationResult.ErrorMessage == target.ErrorMessage)
{
bool movedOld = true;
bool movedTarget = 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)
{
return oldValidationResult;
}
if (movedOld != movedTarget || oldEnumerator.Current != targetEnumerator.Current)
{
break;
}
}
}
}
return null;
}
6. VisualStates
该类是 DataGrid 控件的可视化状态类,主要分类一下几种状态组:
- 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
- ScrollBarsSeparator - SeparatorExpanded,SeparatorCollapsed,SeparatorExpandedWithoutAnimation,SeparatorCollapsedWithoutAnimation
- ScrollBars - TouchIndicator,MouseIndicator,MouseIndicatorFull,NoIndicator
总结
这里我们把 DataGrid 的 Utilities 相关类介绍完成了,作为 DataGrid 相关分享的第二篇,后面我们会继续分享最重要的 DataGrid 的相关重点。
最后,再跟大家安利一下 WindowsCommunityToolkit 的官方微博:https://weibo.com/u/6506046490, 大家可以通过微博关注最新动态。
衷心感谢 WindowsCommunityToolkit 的作者们杰出的工作,感谢每一位贡献者,Thank you so much, ALL WindowsCommunityToolkit AUTHORS !!!
Windows Community Toolkit 4.0 - DataGrid - Part02的更多相关文章
- Windows Community Toolkit 4.0 - DataGrid - Part03
概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part02 中,我们针对 DataGrid 控件的 Utilities 部分做了详细分享.而在 ...
- Windows Community Toolkit 4.0 - DataGrid - Part01
概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Overview 中,我们对 DataGrid 控件做了一个概览的介绍,今天开始我们会做进一步的 ...
- Windows Community Toolkit 4.0 - DataGrid - Overview
概述 Windows Community Toolkit 4.0 于 2018 月 8 月初发布:Windows Community Toolkit 4.0 Release Note. 4.0 版本相 ...
- Windows Community Toolkit 3.0 - UniformGrid
概述 UniformGrid 控件是一个响应式的布局控件,允许把 items 排列在一组均匀分布的行或列中,以填充整体的可用显示空间,形成均匀的多个网格.默认情况下,网格中的每个单元格大小相同. 这是 ...
- Windows Community Toolkit 3.0 - InfiniteCanvas
概述 InfiniteCanvas 是一个 Canvas 控件,它支持无限画布的滚动,支持 Ink,文本,格式文本,画布缩放操作,撤销重做操作,导入和导出数据. 这是一个非常实用的控件,在“来画视频” ...
- Windows Community Toolkit 3.0 - Gaze Interaction
概述 Gaze Input & Tracking - 也就是视觉输入和跟踪,是一种和鼠标/触摸屏输入非常不一样的交互方式,利用人类眼球的识别和眼球方向角度的跟踪,来判断人眼的目标和意图,从而非 ...
- Windows Community Toolkit 3.0 - CameraPreview
概述 Windows Community Toolkit 3.0 于 2018 年 6 月 2 日 Release,同时正式更名为 Windows Community Toolkit,原名为 UWP ...
- Windows Community Toolkit 3.0 新功能 在WinForms 和 WPF 使用 UWP 控件
本文告诉大家一个令人震惊的消息,Windows Community Toolkit 有一个大更新,现在的版本是 3.0 .最大的提升就是 WinForm 和 WPF 程序可以使用部分 UWP 控件. ...
- 与众不同 windows phone (44) - 8.0 位置和地图
[源码下载] 与众不同 windows phone (44) - 8.0 位置和地图 作者:webabcd 介绍与众不同 windows phone 8.0 之 位置和地图 位置(GPS) - Loc ...
随机推荐
- (后端)JackSon将java对象转换为JSON字符串(转)
转载小金金金丶园友: JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的 ...
- Java 一些知识点总结
本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺(阅读本文需要有一定的Java基础).本文的 ...
- mysqldump 参数--lock-tables浅析
mysqldump有一个参数--lock-tables,以前对这个参数也没有详细了解过,直到上次有个网友问"参数lock-tables 是一次性锁定当前库的所有表,还是锁定当前导出表?&qu ...
- 自动化测试基础篇--Selenium Xpath定位
摘自https://www.cnblogs.com/sanzangTst/p/7458056.html 学习 什么是xpath? XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言 ...
- 转:RowVersion 用法
在数据表更新时,如何表征每个数据行更新时间的先后顺序?最简单的做法是使用RowVersion(行版本)字段,它和时间戳(TimeStamp)类型的功能相似,只不过TimeStamp 已过时,应避免用于 ...
- c/c++ 深拷贝
解决上一篇浅拷贝的问题 浅拷贝的问题根源是,类里有指针类型的成员变量,所以需要自己编写拷贝构造函数和重载=函数 #include <iostream> #include <strin ...
- MySQL常用命令(一)
(1)库的基础操作 查看已有库: show databases; 创建库(制定默认字符集): ccreate database 库名 default charset=utf8; 查看创建库的语句: s ...
- C++ 标准 和 C 标准 (截止到2019年03月)
C++ 标准:维基百科 Year C++ Standard Informal name 1998 ISO/IEC 14882:1998[23] C++98 2003 ISO/IEC 14882:200 ...
- 如何让PHP程序自动执行(后台)
如何让php程序自动执行,这个就需要用到一个函数了: int ignore_user_abort ( [bool setting] ) 定义和用法 ignore_user_abort() 函数设置与 ...
- 创建两个SAP系统之间的RFC信任关系
一种常见的场景是企业运行着多个SAP系统(ERP/SRM/CRM),用户希望在AA1系统中使用BB1系统的事务.如果直接使用RFC调用另一系统的事务的话,则会弹出登陆框,让用户再次输入帐号密码... ...