Search code examples
c#xamlavaloniauiavalonia

PointerPressed not working in DataTemplate


I have following xaml:

<ListBox PointerPressed="OnPressed"         
         ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                 <DataTemplate>
                        <Grid PointerPressed="OnPressed">
                            <TextBox Text="{Binding Value}"/>
                        </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

And code behind:

public void OnPressed(object? sender, PointerPressedEventArgs? args)
{
     //some logic to handle PointerPressed event
}

PointerPressed on ListBox works, but PointerPressed on Grid (inside ListBox ItemTeplate) doesn't work, even when I remove PointerPressed from ListBox.

Do I miss something here?


Solution

  • UI elements only receive mouse input in areas where they have non-null drawing content.

    Set Background="Transparent" (or any other Brush) on the Grid to make its whole are hit-test visible.

    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="Transparent" PointerPressed="OnPressed">
                <TextBox Text="{Binding Value}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>