我正在尝试为AutomationProperties.Name
控件模板中的控件设置GroupStyle
属性,但它似乎什么也没产生。我把它设置在我的模板中的Expander
上,但即使我只是放入一些没有绑定的文本,它也没有任何说明。我也尝试在GroupItem
上放置一个setter,但也没有用。我有点失落。我希望小组项目上的属性可以解决它。
XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication8.MainWindow"
x:Name="win"
Title="MainWindow"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot">
<ListBox x:Name="lstbx"
Margin="71,45,99,78"
ItemsSource="{Binding ElementName=win,
Path=Samples}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="AutomationProperties.Name"
Value="this is a test" />
<Setter Property="KeyboardNavigation.TabNavigation"
Value="Cycle" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Name="templateLstBxExpander"
AutomationProperties.Name="test test test"
IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<Label Name="templateLstBxExpanderHeader"
Content="{Binding Path=Name}"
FontWeight="Bold" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</Grid>
</Window>
XAML.cs:
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty sampleProperty = DependencyProperty.Register(
"Samples", typeof(ObservableCollection<sample>), typeof(MainWindow), new PropertyMetadata(new ObservableCollection<sample>()));
public ObservableCollection<sample> Samples
{
get
{
return (ObservableCollection<sample>)this.GetValue(sampleProperty);
}
set
{
this.SetValue(sampleProperty, value);
}
}
public MainWindow()
{
this.InitializeComponent();
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lstbx.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Location");
view.GroupDescriptions.Add(groupDescription);
sample test = new sample();
test.Location = "one";
test.Name = "blah blah";
Samples.Add(test);
sample test2 = new sample();
test2.Location = "two";
test2.Name = "ya ya";
Samples.Add(test2);
}
}
}
sample.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication8
{
public class sample
{
public string Name { set; get; }
public string Location{ set; get; }
}
答案 0 :(得分:0)
尝试将AutomationProperties.HelpText
与Name
一起设置。
答案 1 :(得分:0)
问题在于屏幕阅读器应该如何达到该值。可以通过MSAA从光标访问Groupitems,但不能通过UIA访问。 UIA是用于WPF中可访问性的主要API。
UIA和WPF的核心问题是,当试图读取光标找到的控件时(其他方式是焦点和插入符号),通常会返回主窗口。
作为屏幕阅读器的开发人员,处理此问题的最佳方法是同时使用MSAA和UIA。如果UIA没有返回任何有价值的东西,请回到使用MSAA。
答案 2 :(得分:-2)
您可以使用DisplayMemberPath="Name"
:
<ListBox x:Name="lstbx" Margin="71,45,99,78" ItemsSource="{Binding ElementName=win, Path=Samples}" DisplayMemberPath="Name" >
或者您可以使用.ToString()
:
public class sample
{
public string Name { set; get; }
public string Location { set; get; }
public override string ToString()
{
return Name;
}
}