6
 <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.

Saoirse
  • 237
  • 2
  • 16
  • Does this happen if you use the latest Version of Visual Studio 2017 and .Net Framework? There were improvements and bug fixes in Touch in WPF – Tony May 23 '18 at 13:24
  • I upgraded to vs 2017 targeting the latest framework - 4.7.2. Same issue. Touch events only randomly fire in a list view – Saoirse May 23 '18 at 15:01
  • I don't have a touchscreen to test this, but if I were developing a new Touch App I would use UWP, and its new APIs – Tony May 23 '18 at 15:13
  • 1
    Thanks but unfortunately the touchscreen aspect is new functionality to an existing application. This just seems like an inherent WPF bug – Saoirse May 23 '18 at 16:49

3 Answers3

5
<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.

Saoirse
  • 237
  • 2
  • 16
  • That seems to work! The mouse pointer gets also hidden, no mouseover event. – asdf Jan 14 '19 at 09:55
  • 1
    It did fix the issue with touch not firing sometimes, but as it completely disables touch, other functionalities such as zoom, scrolling don't work anymore. Has anyone found another solution for these issues? – Luishg Sep 05 '19 at 15:47
2

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.
walterlv
  • 2,366
  • 13
  • 73
0

I know this question is rather old, but maybe someone else is coming here looking for a solution to this problem, like me. walterlv desribed the problem very well. If you don't need touch-scrolling Behaviour of ListView and you are interested in the SelectionChanged event, you can add this to your ListView:

<ListView.ItemContainerStyle>
   <Style TargetType="ListViewItem">
      <Style.Triggers>
         <Trigger Property="IsStylusOver" Value="True">
            <Setter Property="IsSelected" Value="True"/>
         </Trigger>
      </Style.Triggers>
   </Style>
</ListView.ItemContainerStyle>

This way, every touch on a ListViewItem reliably selects it and fires a SelectionChanged event.

skyme
  • 11
  • 3