翻譯|使用教程|編輯:龔雪|2022-07-20 10:24:04.740|閱讀 165 次
概述:本文主要為大家介紹如何在使用DevExpress WinForms控件自定義輔助功能屬性,歡迎下載最新版產品體驗哦~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
獲取DevExpress WinForms v22.1正式版下載
這個示例有點復雜,下圖說明了Inspect從"Tree List | Banded Layout"示例中檢索到的數據,結果與上文中的Grid示例中看到的類似:節點和行名稱采用簡單的“Object N”格式。
通過合并父子樹列表節點名稱來放大這些默認名稱,例如,如果用戶將鼠標懸停在根“Sun”節點上,則輔助功能名稱應為“Sun star”。將鼠標懸停在Jupiter節點上應返回行星名稱加上其主要太陽系恒星的名稱:“木星行星太陽星”。行星衛星的完整名稱將采用以下格式:“Io 衛星木星行星太陽星”。下圖說明了正在努力實現的目標。
使用這樣的輔助功能名稱,用戶將永遠不會迷失在帶狀節點的復雜層次結構中。 要設置這些名稱,我們需要相同的 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。
我們通常建議您避免使用內部類的 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 WinForm擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
DevExpress技術交流群6:600715373 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網