AABBbaby

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

看界面控件DevExpress WinForms——如何自定义辅助功能属性(下)

获取DevExpress WinForms v22.1正式版下载

分层可访问性数据

这个示例有点复杂,下图说明了Inspect从"Tree List | Banded Layout"示例中检索到的数据,结果与上文中的Grid示例中看到的类似:节点和行名称采用简单的“Object N”格式。

看界面控件DevExpress WinForms——如何自定义辅助功能属性(下)

通过合并父子树列表节点名称来放大这些默认名称,例如,如果用户将鼠标悬停在根“Sun”节点上,则辅助功能名称应为“Sun star”。将鼠标悬停在Jupiter节点上应返回行星名称加上其主要太阳系恒星的名称:“木星行星太阳星”。行星卫星的完整名称将采用以下格式:“Io 卫星木星行星太阳星”。下图说明了正在努力实现的目标。

看界面控件DevExpress WinForms——如何自定义辅助功能属性(下)

使用这样的辅助功能名称,用户将永远不会迷失在带状节点的复杂层次结构中。 要设置这些名称,我们需要相同的 QueryAccessibleInfo 事件,以及一个自定义方法,该方法接收一个节点并开始向上移动,直到它到达最顶层的父节点,在此过程中合并节点名称。

 

using DevExpress.Accessibility;

public MyForm() {
InitializeComponent();
// ...
DXAccessible.QueryAccessibleInfo += (s, e) => {
if (e.OwnerControl == treeList1) {
if (e.Role == AccessibleRole.OutlineItem && e.Owner is TreeListNode)
e.Name = GetNodeAccessibleName((TreeListNode)e.Owner);
}
};
}

// Obtain the topmost parent and merge all parent node names
string GetNodeAccessibleName(TreeListNode node) {
TreeListNode currentNode = node;
string name = "";
while (currentNode != null) {
if (name != "")
name += " ";
name += currentNode.GetDisplayText("Name");
name += " " + currentNode.GetDisplayText("TypeOfObject");
currentNode = currentNode.ParentNode;
}
return name;
}

 

此代码示例可以解决问题,但我们只修改了节点名称,单元格仍会返回名称,例如“Mass row 1”或“Volume row 5”。 这里的问题是我们无法立即修改单元名称,因为无法确定哪个节点拥有当前单元,事件属性不向我们提供此信息。但这里有一个技巧:如果您在 GetNodeAccessibleName 事件处理程序中添加断点并调用 e.GetDXAccessible<BaseAccessible>() 方法,可以获得内部 BaseAccessible 类的后代,该类返回有关 UI 元素的信息。 在树列表单元的情况下,后代是 TreeListAccessibleRowCellObject。

看界面控件DevExpress WinForms——如何自定义辅助功能属性(下)

我们通常建议您避免使用内部类的 API,因为不保证与未来版本的兼容性,但是如果使用类定义(Visual Studio 中的 F12), 您将看到 TreeListAccessibleRowCellObject 从 System.Runtime.InteropServices 命名空间实现 IGridItemProvider 接口,可以安全地假设这个接口是稳定的并且不会受到未来变化的影响,因此可以利用其 Column 和 Row 属性来识别当前单元格的父级。

 

using DevExpress.Accessibility;
using DevExpress.UIAutomation;

DXAccessible.QueryAccessibleInfo += (s, e) => {
if (e.OwnerControl == treeList1) {
// ...
if (e.Role == AccessibleRole.Cell && e.GetDXAccessible<BaseAccessible>() is IGridItemProvider) {
string cellName = GetCellAccessibleName((IGridItemProvider)e.GetDXAccessible<BaseAccessible>());
if (cellName != null)
e.Name = cellName;
}
}
};

string GetCellAccessibleName(IGridItemProvider gridItemProvider) {
TreeListNode node = treeList1.GetNodeByVisibleIndex(gridItemProvider.Row);
if (node != null && treeList1.Columns[gridItemProvider.Column] != null)
return GetNodeAccessibleName(node) + " " + treeList1.Columns[gridItemProvider.Column].Caption;
return null;
}

 

下图说明了最终结果(单个单元格元素突出显示)。

看界面控件DevExpress WinForms——如何自定义辅助功能属性(下)

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!


DevExpress技术交流群6:600715373      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

posted on   AABBbaby  阅读(34)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-07-21 手把手教你创建一个Windows风格的应用程序界面(Part 1)
2021-07-21 Web应用的数据管理教程 - 电子表格如何支持图像?
2020-07-21 WPF界面开发必看功能——Splash Screen Manager
点击右上角即可分享
微信分享提示