翻譯|使用教程|編輯:龔雪|2022-07-20 10:24:04.740|閱讀 165 次
概述:本文主要為大家介紹如何在使用DevExpress WinForms控件自定義輔助功能屬性,歡迎下載最新版產(chǎn)品體驗哦~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
獲取DevExpress WinForms v22.1正式版下載
這個示例有點(diǎn)復(fù)雜,下圖說明了Inspect從"Tree List | Banded Layout"示例中檢索到的數(shù)據(jù),結(jié)果與上文中的Grid示例中看到的類似:節(jié)點(diǎn)和行名稱采用簡單的“Object N”格式。
通過合并父子樹列表節(jié)點(diǎn)名稱來放大這些默認(rèn)名稱,例如,如果用戶將鼠標(biāo)懸停在根“Sun”節(jié)點(diǎn)上,則輔助功能名稱應(yīng)為“Sun star”。將鼠標(biāo)懸停在Jupiter節(jié)點(diǎn)上應(yīng)返回行星名稱加上其主要太陽系恒星的名稱:“木星行星太陽星”。行星衛(wèi)星的完整名稱將采用以下格式:“Io 衛(wèi)星木星行星太陽星”。下圖說明了正在努力實現(xiàn)的目標(biāo)。
使用這樣的輔助功能名稱,用戶將永遠(yuǎn)不會迷失在帶狀節(jié)點(diǎn)的復(fù)雜層次結(jié)構(gòu)中。 要設(shè)置這些名稱,我們需要相同的 QueryAccessibleInfo 事件,以及一個自定義方法,該方法接收一個節(jié)點(diǎn)并開始向上移動,直到它到達(dá)最頂層的父節(jié)點(diǎn),在此過程中合并節(jié)點(diǎn)名稱。
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; }
此代碼示例可以解決問題,但我們只修改了節(jié)點(diǎn)名稱,單元格仍會返回名稱,例如“Mass row 1”或“Volume row 5”。 這里的問題是我們無法立即修改單元名稱,因為無法確定哪個節(jié)點(diǎn)擁有當(dāng)前單元,事件屬性不向我們提供此信息。但這里有一個技巧:如果您在 GetNodeAccessibleName 事件處理程序中添加斷點(diǎn)并調(diào)用 e.GetDXAccessible<BaseAccessible>() 方法,可以獲得內(nèi)部 BaseAccessible 類的后代,該類返回有關(guān) UI 元素的信息。 在樹列表單元的情況下,后代是 TreeListAccessibleRowCellObject。
我們通常建議您避免使用內(nèi)部類的 API,因為不保證與未來版本的兼容性,但是如果使用類定義(Visual Studio 中的 F12), 您將看到 TreeListAccessibleRowCellObject 從 System.Runtime.InteropServices 命名空間實現(xiàn) IGridItemProvider 接口,可以安全地假設(shè)這個接口是穩(wěn)定的并且不會受到未來變化的影響,因此可以利用其 Column 和 Row 屬性來識別當(dāng)前單元格的父級。
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; }
下圖說明了最終結(jié)果(單個單元格元素突出顯示)。
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)