Skip to content

ListBox DoubleTapped event has inconsistent SelectedIndex #20923

Description

@pygmypenguin

Describe the bug

When double tapping a listboxitem with a mouse, the SelectedIndex of the sender is the index of the item that was tapped. When this same item is double tapped with a pen or other touch input, though, the SelectedIndex is -1, as if the item had been deselected just before firing the DoubleTapped event.

To Reproduce

Create the following types and compose an avalonia demo app. Then, double tap with a mouse and touch device on any ListBoxItem.

MyControl.axaml

<UserControl xmlns="https://github.com/avaloniaui"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                      xmlns:local="using:Demo"
                      mc:Ignorable="d"
                      x:Class="Demo.MyControl"
                      x:DataType="local:DemoViewModel">
    <StackPanel Orientation="Vertical">
        <ListBox DoubleTapped="MyListBox_DoubleTapped" ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="local:DemoItem">
                    <Border>
                        <TextBlock Text="{Binding Content}" />
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock x:Name="ResultText" />
    </StackPanel>
</UserControl>

MyControl.axaml.cs

using Avalonia.Controls;
using Avalonia.Input;

namespace Demo;

public partial class MyControl : UserControl {
    public MyControl() {
        InitializeComponent();
    }

    public void MyListBox_DoubleTapped(object? sender, TappedEventArgs e) {
        if (sender is not ListBox lb) return;
        if (lb.SelectedIndex < 0) {
            ResultText.Text = "No Index Selected on DoubleTap";
        }
        else {
            ResultText.Text = $"Selected Index {e.SelectedIndex} with DoubleTap";
        }
    }
}

DemoViewModel.cs

using System.Collections.ObjectModel;

namespace Demo;

public record Item(string Content);

public class DemoViewModel {
    public ObservableCollection<Item> Items { get; } = [
        new Item("Item 1"),
        new Item("Item 2")
    ];
}

Expected behavior

The ResultText TextBlock should show "Selected Index with DoubleTap" with both a mouse selection and a touch device selection.

Avalonia version

11.3.9

OS

Windows

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions