Search code examples
wpflistviewlistviewitemdouble-click

DoubleClick not handled on ListViewItem when clicking between Labels which comprises the ListViewItem


I implemented a ListView with some ListViewItems. A ListViewItem is comprised of two labels in a grid. In order to not having an underscore eat a letter, I've implemented a fix, setting RecognizesAccessKey to false.

<Grid.Resources>
    <Style x:Key="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                          Margin="5"   
                                          RecognizesAccessKey="False" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Grid.Resources>

I'd like to handle a double-click on a ListViewItem. Adding it to the InputBindings of the ListView doesn't work. There's another question here on StackOverflow discussing this issue.

The answer to that other question suggests to move it to the InputBindings of the ListViewItem itself. This does generally work, but when clicking between the Labels, the double-click is not handled.
This is caused by the code shown above. Removing the Margin and having both Labels next to each other would be a 'solution' but I'd like to have them to close to each other. Of course, removing the code above altogether fixes the issue, too.
But with this margin, there is some space between both Labels that do not handle the DoubleClick. This wouldn't change, if the Margin is set on the Labels directly.

A completely different solution, suggested on yet another question, is to implement an attached behavior to the ListView. And yes, then it doesn't matter where I click, the DoubleClick is handled. However, it's even handled when not clicking on an item but somewhere in the ListView. The ListView usually has some empty space below the last item and when clicking there, the DoubleClick is still handled. I would like to handle it only when an item is hovered.

I don't have any strong preference regarding where the DoubleClick is implemented (i.e. both ListView and ListViewItem is fine), but I'm not sure how to get either working fine. Both implementations have that one downside and I haven't figured out how to get around it.

Does anyone have an idea on how to get one of the two approaches above working.


Solution

  • It turned out that the Border has a BackgroundColor of Null by default. When setting it to Transparent, the double-click is handled.

    <Border Background="Transparent">
    

    Links:
    Mouse event on transparent background
    {x:Null} vs. Transparent?
    Hit Testing in the Visual Layer