C# 将动态automationID绑定到ListView中的每个ListViewItem

C# 将动态automationID绑定到ListView中的每个ListViewItem,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,在我公司的当前项目中,我正在尝试为WPF应用程序中的每个UI元素设置自动化ID 在ListView中有几个ListViewItems,我希望通过AutomationID可以访问它们。因此,我试图为ListView中的每个ListViewItem设置一个automationID,它看起来像“dataitem1”、“dataitem2”、“dataitem3”等等 以下是我如何使用XAML代码获得的: <ListView IsEnabled="{Binding TestSelecto

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

在ListView中有几个ListViewItems,我希望通过AutomationID可以访问它们。因此,我试图为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>
<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>

但对于每个ListViewItem,这都会导致以下错误:

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

所以我试着和相对资源一起玩,就像这样:

<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>
 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>

这让我产生了这个错误:

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


有人能告诉我我做错了什么吗?

也许你忘了给你的窗口提供正确的数据上下文

它可能是这样的:

<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>
 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>
xmlns:vm=“clr命名空间:视图模型”
d:DataContext=“{d:DesignInstance Type=vm:TestsViewModel,IsDesignTimeCreatable=True}”

可能您忘记为窗口提供正确的数据上下文

它可能是这样的:

<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>
 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>
xmlns:vm=“clr命名空间:视图模型”
d:DataContext=“{d:DesignInstance Type=vm:TestsViewModel,IsDesignTimeCreatable=True}”

下面是我的工作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>
<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>

下面是我的工作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>
<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>


事实上,是datacontext设置不正确。我将在下面发布我的解决方案,它太长了,无法发表评论。事实上,是datacontext设置不正确。我将在下面发布我的解决方案,太长了,无法发表评论