【问题标题】:Binding a dynamic automationID to every ListViewItem in a ListView将动态自动化 ID 绑定到 ListView 中的每个 ListViewItem
【发布时间】:2021-03-29 04:10:19
【问题描述】:

在我当前的公司项目中,我尝试为我的 WPF 应用程序中的每个 UI 元素设置自动化 ID。

在 ListView 内,有几个 ListViewItems 我希望通过自动化 ID 访问它们。所以我试图为 ListView 中的每个 ListViewItem 设置一个automationID,看起来像“dataitem1”、“dataitem2”、“dataitem3”等。

这就是我使用 XAML 代码的方式:

<ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1" DataContext="{Binding TestsViewModel.TestsDataTable}" ItemsSource="{Binding TestsViewModel.TestsDataTable}" SelectedValue="{Binding TestsViewModel.SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding TestsViewModel.GridView}"  TabIndex="0" MinWidth="1" Focusable="False">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="AutomationProperties.AutomationId" Value="{Binding TestsViewModel.ListItemCounter, StringFormat='dataitem{}{0}'}"/>
                </Style>
            </ListView.ItemContainerStyle>
</ListView>

但是对于每个 ListViewItem,这都会给我这个错误:

System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“DataRowView”(HashCode=33913349)上找不到“TestsViewModel”属性。 BindingExpression:Path=TestsViewModel.ListItemCounter; DataItem='DataRowView' (HashCode=33913349);目标元素是'ListViewItem'(名称='');目标属性是“AutomationId”(类型“字符串”)

所以我尝试了这样的 RelativeSource:

<ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1" ItemsSource="{Binding TestsViewModel.TestsDataTable}" SelectedValue="{Binding TestsViewModel.SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding TestsViewModel.GridView}"  TabIndex="0" MinWidth="1" Focusable="False">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="AutomationProperties.AutomationId" Value="{Binding TestsViewModel.ListItemCounter, StringFormat='dataitem{}{0}', RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"/>
                </Style>
            </ListView.ItemContainerStyle>
</ListView>

这让我遇到了这个错误:

System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ListViewItem',AncestorLevel='1'' 绑定的源。 BindingExpression:Path=TestsViewModel.ListItemCounter;数据项=空;目标元素是'ListViewItem'(名称='');目标属性是“AutomationId”(类型“字符串”)

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# .net wpf xaml data-binding


    【解决方案1】:

    也许您忘记为窗口提供正确的数据上下文。

    它可能看起来像这样:

     xmlns:vm="clr-namespace:ViewModels"            
     d:DataContext="{d:DesignInstance Type=vm:TestsViewModel, IsDesignTimeCreatable=True}"
    
    <ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1" DataContext="{Binding TestsViewModel}" ItemsSource="{Binding TestsDataTable}" SelectedValue="{Binding SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding GridView}"  TabIndex="0" MinWidth="1" Focusable="False">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="AutomationProperties.AutomationId" Value="{Binding ListItemCounter, StringFormat='dataitem{}{0}'}"/>
                    </Style>
                </ListView.ItemContainerStyle>
    </ListView>
    

    【讨论】:

    • 确实是数据上下文设置不正确。我将在下面发布我的解决方案,评论太长了
    【解决方案2】:

    在我的工作 XAML 代码下方:

    <ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1"  ItemsSource="{Binding TestsDataTable}" SelectedValue="{Binding SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding GridView}"  TabIndex="0" MinWidth="1" Focusable="False">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="AutomationProperties.AutomationId" Value="{Binding Path=DataContext.TestsViewModel.ListItemCounter, StringFormat='dataitem{0:F0}', UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"/>
                    </Style>
                </ListView.ItemContainerStyle>
    </ListView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多