【问题标题】:Making a grouped WPF ItemsControl accessible to screen readers使屏幕阅读器可以访问分组的 WPF ItemsControl
【发布时间】:2021-01-21 08:44:50
【问题描述】:

我的 WPF 应用程序中有一个 ItemsControl。此应用程序必须可供屏幕阅读器软件访问,例如讲述人、NVDA 和 JAWS。

问题在于,当使用分组时,ItemsControl 中的子项突然变得对屏幕阅读器不可见。如果我删除分组,它们就会出现。

如何对屏幕阅读器进行分组和查看?

【问题讨论】:

    标签: .net wpf accessibility itemscontrol


    【解决方案1】:

    WPF UI 元素可以向屏幕阅读器公开“自动化对等点”。

    默认情况下,WPF 不会为ItemsControl 提供对等点,但它会为派生控件(例如ListBox)提供。

    要解决这个问题,您可以创建自己的派生控件并让它返回一个自动化对等点。

    using System.Windows.Automation.Peers;
    using System.Windows.Controls;
    
    public sealed class AccessibleItemsControl : ItemsControl
    {
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new AccessibleItemsControlAutomationPeer(this);
        }
    
        private sealed class AccessibleItemsControlAutomationPeer : ItemsControlAutomationPeer
        {
            public AccessibleItemsControlAutomationPeer(ItemsControl owner)
                : base(owner)
            {
            }
    
            protected override ItemAutomationPeer CreateItemAutomationPeer(object item)
            {
                return new AccessibleItemAutomationPeer(item, this);
            }
    
            protected override string GetClassNameCore() => "AccessibleItemsControl";
    
            protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.List;
        }
    
        private sealed class AccessibleItemAutomationPeer : ItemAutomationPeer
        {
            public AccessibleItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer)
                : base(item, itemsControlAutomationPeer)
            {
            }
    
            protected override string GetClassNameCore() => "AccessibleItemsControlItem";
    
            protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.DataItem;
        }
    }
    

    您可以使用如下代码在 XAML 中指定项目和组的自动化名称:

    <controls:AccessibleItemsControl>
      <ItemsControl.ItemContainerStyle>
        <Style>
          <!-- customise the binding path here to suit your application -->
          <Setter Property="AutomationProperties.Name" Value="{Binding Path=MyDisplayName}" />
        </Style>
      </ItemsControl.ItemContainerStyle>
      <ItemsControl.GroupStyle>
        <GroupStyle>
          <GroupStyle.ContainerStyle>
            <Style>
              <!-- customise the binding path here to suit your application -->
              <Setter Property="AutomationProperties.Name" Value="{Binding Path=(CollectionViewGroup.Name).MyDisplayName}" />
            </Style>
          </GroupStyle.ContainerStyle>
        </GroupStyle>
      </ItemsControl.GroupStyle>
    </controls:AccessibleItemsControl>
    

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2018-12-31
      相关资源
      最近更新 更多