jcq jcq - 2 months ago 12
Vb.net Question

WPF with touchscreen, listbox opens a modal window doesn't respond

I'm facing a problem since some weeks/months, i hope someone can help me ;)
This WPF problem appears only when using via the touch screen, it works fine with mouse.
The concept to reproduce is very simple:
- 1 window with a listbox or listview
- You populate the list with simple item (just a text for example)
- A second window with 2 or more simple buttons

When you select an item in the list in window 1 via touch screen, it opens in modal mode (ShowDialog) the second window. When the second window is open you can't touch any button at first touch!
If you do the same with the mouse it is working fine! It looks like the touch event is not finished when you call the window 2 ShowDialog (it works fine when it is not modal: using Show).

Here the XAML window 1:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="661" WindowStartupLocation="CenterScreen">
<StackPanel>
<ListView x:Name="lstItems" Height="300" Margin="10,10,20,10" Width="Auto" ItemsSource="{Binding}" BorderBrush="{x:Null}"
SelectionMode="Single" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Hidden">
</ListView>
</StackPanel>
</Window>


Here the code behind for window 1:

Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
With lstItems
.Items.Clear()
For i = 0 To 50
.Items.Add("test #" & i.ToString)
Next
End With
End Sub

Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged
Dim _frm As frm2 = New frm2
_frm.ClickedItem = lstItems.SelectedItem.ToString
_frm.ShowDialog()
_frm.Close()
_frm = Nothing
End Sub

End Class


Here the window 2 xaml:

<Window x:Class="frm2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="frm2" Height="393" Width="661" WindowStartupLocation="CenterScreen">
<Grid>
<UniformGrid Columns="2">
<Button x:Name="cmdbutton1" Height="128" Content="Button1"/>
<Button x:Name="cmdbutton2" Height="128" Content="Button2"/>
</UniformGrid>
</Grid>
</Window>


Here the code behind for window 2:

Public Class frm2
Property ClickedItem As String = ""

Private Sub frm2_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Me.Title = ClickedItem
End Sub

Private Sub cmdbutton1_Click(sender As Object, e As RoutedEventArgs) Handles cmdbutton1.Click
Me.Close()
End Sub

Private Sub cmdbutton2_Click(sender As Object, e As RoutedEventArgs) Handles cmdbutton2.Click
Me.Close()
End Sub
End Class


Thank you in advance for your help...

jcq jcq
Answer

This problem is referenced in Microsoft KB, it has been marked as solved since .NET 4.5.1, anyway it is not as i can reproduce it easily... I just found a workaround here:

Just replace original code:

    Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged
    Dim _frm As frm2 = New frm2
    _frm.ClickedItem = lstItems.SelectedItem.ToString
    _frm.ShowDialog()
    _frm.Close()
    _frm = Nothing
End Sub

With this one:

    Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged

            Dispatcher.BeginInvoke(Sub()

    Dim _frm As frm2 = New frm2
    _frm.ClickedItem = lstItems.SelectedItem.ToString
    _frm.ShowDialog()
    _frm.Close()
    _frm = Nothing

End Sub) End If End Sub