我怀疑你得不到答案,因为没有人能重复这个问题。事实上,只要使用您提供的代码,您就可以单击网格中的任意位置,并且该项将被选中。
我猜您的团队中有人以您可能知道或不知道的样式模板化了ListViewItem。如果使用ListViewItem模板,则可以看到所讨论的问题。
如果运行下面的代码,您将看到问题:
<Grid>
<ListView Grid.Row="1"
x:Name="lvUsers"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding AllUsers,Mode=TwoWay}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<Rectangle x:Name="ItemBackground" Fill="{x:Null}"/>
<GridViewRowPresenter/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ItemBackground" Property="Fill" Value="LightGray" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBackground" Property="Fill" Value="Gray" />
<Setter TargetName="ItemBackground" Property="StrokeDashArray" Value="1 1 1"/>
<Setter TargetName="ItemBackground" Property="Stroke" Value="DarkGray"/>
<Setter TargetName="ItemBackground" Property="StrokeThickness" Value="1"/>
<Setter TargetName="ItemBackground" Property="RenderOptions.EdgeMode" Value="Aliased"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.Items>
<ListViewItem Content="Test Item 1"></ListViewItem>
<ListViewItem Content="Test Item 2"></ListViewItem>
<ListViewItem Content="Test Item 3"></ListViewItem>
<ListViewItem Content="Test Item 4"></ListViewItem>
</ListView.Items>
<ListView.View>
<GridView>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding}"/>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding}"/>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
解决方案是在整个项目模板上添加透明度为0的矩形,如下所示:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<Rectangle x:Name="ItemBackground" Fill="{x:Null}"/>
<GridViewRowPresenter/>
**<Rectangle Fill="White" Opacity="0"></Rectangle>**
</Grid>
< etc... >
现在单击可以工作,悬停也可以。