Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListViewItem events not firing properly on Touchscreen

 <Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowState="Maximized"
        Title="MainWindow" Height="550" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="4*"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBox Name="TextBox" VerticalContentAlignment="Center" FontSize="30" ></TextBox>
        <ListView Grid.ColumnSpan="6"  Grid.Row="1"
                     x:Name="GridControlProducts"
                    SelectionMode="Single"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Width" Value="200"/>
                    <Setter Property="Height" Value="200"/>
                    <EventSetter Event="PreviewMouseDown" Handler="button_Click" />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>       


        <Button Content="Close" Grid.Column="0" Grid.Row="2" Click="Button_Click_1" ></Button>

    </Grid>
</Window>
// code behind
public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                this.GridControlProducts.Items.Add("Test");
            }
        }

 private int c = 1;
        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.TextBox.Text = this.c++.ToString();
        }

I have a WPF touchscreen application which contains a list of products in a list view template.

Using a mouse everything works as you would expect. However, on a touchscreen the touch events don't fire every time. For example, if I press 10 buttons in my list view in a row, maybe 7 touches will register and 3 touches won't.

If I touch a standard button on it's own, it's very responsive. The buttons in my template are not (very hit and miss).

I created a simple test application (see above) to test this, and the behaviour on my test app is the same.

When it doesn't register, the previously selected listviewitem is still selected, and the item I have selected that hasn't registered is a light blue (like when you hover over with a mouse)

Any help would be greatly appreciated.

like image 853
Saoirse Avatar asked May 23 '18 12:05

Saoirse


2 Answers

<configuration>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.Windows.Input.Stylus.DisableStylusAndTouchSupport=true" />
  </runtime>
</configuration>

Finally found a fix. I put the above in my config file, and issue resolved.

Thanks for all the responses.

like image 51
Saoirse Avatar answered Sep 23 '22 07:09

Saoirse


Summary

  • Never use Click event in ListView/ListBox, use PreviewMouseDown instead.
  • Your touch device may have a low touch precision than most of the others.

Details

I was suffering from the same issue several years ago and found that it behaviors different on different touch devices. On most touch devices, the touch works every time. But on some others, touch may be lost randomly.

If you try to notice your movement tapping on the touchscreen, you may find that the touch will be lost if your finger moves a very little distance away on pressing. This behavior only happens on ListViewItem or ListBoxItem because the ListView and the ListBox handle the touch events and reraises them for their touch-scrolling behavior.

MouseDown and MouseUp on the same control makes a Click event, so are the touch events. But on a ListBox or a ListView, the click events must be in the same point instead of the same control. Because that the ListView/ListBox has no need to handle mouse events first, so the mouse click works fine.

What makes the difference between different kind of touch devices is the touch precision of them. The lower the precision is, the more touch events will be lost.

What you can do is:

  • Never use Click event in ListView/ListBox, use PreviewMouseDown instead.
like image 40
walterlv Avatar answered Sep 23 '22 07:09

walterlv