We want to select a row on a mouseclick anywhere in that row. Currently the user has to click the text in the row to select the row.
This is our ListView inside a Grid, with a GridView inside it:
<ListView Grid.Row="1"
x:Name="lvUsers"
PreviewMouseDoubleClick="lvUsers_PreviewMouseDoubleClick"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding AllUsers,Mode=TwoWay}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.View>
<GridView>
<GridViewColumn Header="Username"
Width="150"
DisplayMemberBinding="{Binding UserDTO.Name}" />
<GridViewColumn Header="Fullname"
Width="150"
DisplayMemberBinding="{Binding UserDTO.FullName}" />
<GridViewColumn Header="Roles"
Width="250"
DisplayMemberBinding="{Binding Roles}" />
<GridViewColumn Header="Default station"
Width="200" DisplayMemberBinding="{Binding UserDTO.DefaultStation.StationName}"/>
</GridView>
</ListView.View>
</ListView>
How can we get it to select the row when the user clicks anywhere in the row (even on the empty space between say the FullName and the Roles)?
Thanks!
Addendum to Nigel Shaws answer, which lead to an answer to this issue for me.
You don't need to put the
Rectangle
over the entireTemplate
. This will block any controls you may have in your row. Instead you can simply use aRectangle
behind yourGridViewRowPresenter
. Just make sure theFill
property of theRectangle
is set.I suspect you're not getting an answer because nobody can duplicate the problem. With just the code you've supplied in fact you can click anywhere in the grid and the item will be selected.
I'm guessing that someone on your team has templated the ListViewItem in a style that you may or may not know about. If you template ListViewItem then you can see the problem that you're talking about.
If you run the code below you'll see the problem:
In this case, clicking in the white space exhibits the problem behaviour. It doesn't trigger selection of the list item.
The solution is to add a rectangle with transparency 0 over the entire item template, like this:
Now the click works, and the hover as well.
I hope this helps. It drove us crazy too until we thought of it.