xaml - AutomationProperties.HelpText 不适用于 UWP ListView

标签 xaml uwp

我有一个像这样的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>

当我尝试使用“讲述人”时,它会读取艺术家姓名,但不会读取帮助文本。另外,我想知道是否可以将两个值绑定(bind)为 AutomationProperties.Name?

例如,我有一个 ArtistName 和一个 SongName,然后使用 AutomationProperties.Name = "{Binding ArtistName};{Binding SongName}" 然后它会读取类似 Artist (小停顿) SongName 的内容.

最佳答案

When I try to use Narrator it reads the Artist name but it doesn't read help text.

设置AutomationProperties.HelpText attached property DataTemplate 内的内容在这里不起作用。为了解决这个问题,我们可以使用自定义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.NameAutomationProperties.HelpText 不再位于 Grid 中。更多信息请参见XAML accessibility sample .

I want to know is it possible to bind two values a AutomationProperties.Name?

UWP 没有开箱即用的多重绑定(bind)支持。但是,如果 ArtistNamesongName 来自一个模型或 View 模型,那么我们可以使用转换器来实现这一点,如下所示:

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();
    }
}

并使用转换器,如下所示:

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.");
    }
}

关于xaml - AutomationProperties.HelpText 不适用于 UWP ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39043795/

相关文章:

c# - 如何在 Windows 10 XAML 中模糊图像?

c# - 在 Universal App 中等待 5 秒

xamarin - 如何以编程方式隐藏 UWP 应用程序的键盘?

wpf - 单击按钮时将填充设置为 LinearGradientBrush

wpf - 跨 ViewModel 共享状态/更改

c# - 如何在 Windows Phone 8 中更改数据透视表头模板

wpf - 如何使用带有可编辑组合框的 EventToCommand 将 TextBoxBase.TextChanged 与命令绑定(bind)?

wpf - 带有静态文本和绑定(bind)的标签

c# - EF7 UWP SQLite .Include() 方法

c# - UWP MVVM : refresh page after change of language