我有一个DataGrid
绑定到对象集合的 WPF 控件。一切都按原样呈现在屏幕上。
ToString()
由于应用程序中其他地方的要求已被覆盖。
问题是,当通过屏幕阅读器(例如 Microsoft 的内置讲述人)阅读时,或者通过 AccChecker / Inspect 等工具进行检查时,控件的值name
是被覆盖的ToString
值。
我希望能够为屏幕阅读器指定一个描述性名称,但我找不到这样做的方法。我尝试过设置AutomationProperties.Name
,AutomationProperties.ItemType
等,但其中的所有属性AutomationProperties
似乎都没有达到预期的效果。
最理想的情况是,我既可以对数据项本身执行此操作,也可以对列的各个成员执行此操作。
这是我遇到的问题的完整演示:
<DataGrid x:Name="dgTest" ItemsSource="{Binding}" AutoGenerateColumns="false" AutomationProperties.Name="Test">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" IsReadOnly="True" Width="2*" AutomationProperties.Name="Test2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="ID" IsReadOnly="True" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Id}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
和代码:
public class FooItem
{
public Guid Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Concat(Id.ToString(), " : ", Name);
}
}
public partial class MainWindow : Window
{
public readonly List<FooItem> fooList = new List<FooItem>();
public MainWindow()
{
fooList.Add(new FooItem { Id = Guid.NewGuid(), Name = "Test 1" });
fooList.Add(new FooItem { Id = Guid.NewGuid(), Name = "Test 2" });
fooList.Add(new FooItem { Id = Guid.NewGuid(), Name = "Test 3" });
InitializeComponent();
dgTest.DataContext = fooList;
}
}
为了完整起见,这里是 Inspector 的截图。
全尺寸图像