我有一个这样的ListView:
<ListView x:Name="ArtistsList"
ItemsSource="{Binding Source={StaticResource ArtistsCVS}}"
SelectionMode="None"
ItemContainerStyle="{StaticResource ListViewContainerStrecher}"
IsItemClickEnabled="True"
ItemClick="ArtistsList_ItemClick">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:DAPP">
<Grid AutomationProperties.Name="{Binding artist}"
AutomationProperties.HelpText="Navigate to artist info page.">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
MaxWidth="60"
Source="{Binding thumb}"
HorizontalAlignment="Center"/>
<Grid Grid.Column="1" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding artist}"/>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当我尝试使用“讲述人”时,它会读取“艺术家”名称,但不会读取帮助文本。另外,我想知道是否可以将两个值绑定为AutomationProperties.Name?
例如,我有一个ArtistName和一个SongName,然后例如使用
AutomationProperties.Name = "{Binding ArtistName};{Binding songName}"
,那么它会读取类似于Artist(小停顿)SongName的内容。 最佳答案
当我尝试使用“讲述人”时,它会读取“艺术家”名称,但不会读取帮助文本。
在DataTemplate
中设置AutomationProperties.HelpText attached property在这里不起作用。为了解决此问题,我们可以使用自定义ListView和重写PrepareContainerForItemOverride方法来设置自动化属性。这也是向ListView
中的项目添加可访问性支持的推荐方法。
例如:
public class MyList : ListView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(AutomationProperties.NameProperty, new Binding
{
Path = new PropertyPath("Content.artist"),
RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }
});
AutomationProperties.SetHelpText(source, "Navigate to artist info page.");
}
}
然后,您可以使用
MyList
代替ListView
,并且不再需要在AutomationProperties.Name
中设置AutomationProperties.HelpText
和Grid
。有关更多信息,请参见XAML accessibility sample。我想知道是否可以将两个值绑定为AutomationProperties.Name?
UWP没有开箱即用的多绑定支持。但是,如果
ArtistName
和songName
来自一个模型或视图模型,那么我们可以使用Converter来实现这一点,例如:public class AutomationPropertiesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//Suppose ArtistName and songName are the properties of Song class
var song = (Song)value;
return $"{song?.ArtistName} - {song?.songName}";
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
并像这样使用Converter:
public class MyList : ListView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
//Suppose Song class is the DataType of the DataTemplate
source.SetBinding(AutomationProperties.NameProperty, new Binding
{
Path = new PropertyPath("Content"),
RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self },
Converter = new AutomationPropertiesConverter()
});
AutomationProperties.SetHelpText(source, "Navigate to artist info page.");
}
}