3

WPF 4.0:

I have scroll viewer with many Sliders inside of it. I want the scroll viewer to pan with touch, and I want the internal slider to also respond to touch.

Unfortunately, the scroll viewer is eating the "TouchMove" events and not passing them down to the slider control. Any idea how to fix this?

Here is my XAML:

<Window x:Class="ScrollingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl ItemsSource="{Binding}">
            <ItemsControl.Template>
                <ControlTemplate>
                    <ScrollViewer VerticalScrollBarVisibility="Auto" PanningMode="Both" >
                        <ItemsPresenter />
                    </ScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Height="100" BorderThickness="2" BorderBrush="Black">
                        <Slider Value="{Binding ., Mode=TwoWay}" Width="300" Minimum="0" Maximum="100" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

And my Code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = Items;
    }

    public IEnumerable<int> Items
    {
        get
        {
            return Enumerable.Range(0, 50);
        }
    }
}
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167

4 Answers4

2

Please see my answer to this question: ScrollViewer in a touch interface not working properly

I just resolved this issue in our app as well using a custom Thumb control - in my answer I explain what is causing the problem.

Community
  • 1
  • 1
Valerie
  • 1,205
  • 12
  • 16
0

This sounds like a case of "routed event marked as handled". Can you try using AddHandler to subscribe to that event, and set the last parameter "handledEventsToo" to true?

Cheers, Laurent

LBugnion
  • 6,672
  • 2
  • 24
  • 28
  • Good suggestion, Laurent. I tried that, but I still never get the TouchMove event. I've tried overriding the OnTouchMove as well... but it never sets "Handled" to true in the base... blerg. – Brian Genisio Jul 15 '10 at 19:10
  • I'm new at the touch gesture stuff; would you have to register an event handler once you have a TouchDown event? Or does the system depend on certain hardware to fire TouchMove, which you may not have? Just guesses. – Rob Perkins Jul 15 '10 at 21:23
  • When the slider is not in a scroll viewer, everything works properly because the touch events get translated to mouse events. I can even get the TouchMove event without the scroll viewer. BUT, once the scroll viewer takes over, it doesn't let the TouchMove event get down, so it never gets the chance to get translated as a Mouse move. – Brian Genisio Jul 15 '10 at 22:07
0

It's handling the TouchMove event, most likely. There are bubbling events (PreviewTouchMove, etc) you could handle in your Slider control. You would need to coordinate how you want the touch events to be handled.

Rob Perkins
  • 3,088
  • 1
  • 30
  • 51
  • As far as I can tell, the "Handled" flag never gets set to True for PreviewTouchMove OR TouchMove. Still, the events never make their way down to the slider. TouchDown DOES, however, make it down. I've tried CaptureTouch when this happens... but still no luck :( – Brian Genisio Jul 15 '10 at 19:17
0

You can try to make your custom class, derived from ScrollViewer and override OnTouchMove method.

public class CustomScrollViewer : System.Windows.Controls.ScrollViewer
{
    protected override void OnTouchMove(System.Windows.Input.TouchEventArgs e)
    {
        // delete the base.OnTouchMove() call to prevent event being "eat" :)
    }
}

Then, you edit the xaml like this:

<Window x:Class="ScrollingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl ItemsSource="{Binding}">
            <ItemsControl.Template>
                <ControlTemplate>
                    <local:CustomScrollViewer VerticalScrollBarVisibility="Auto" PanningMode="Both" >
                        <ItemsPresenter />
                    </local:CustomScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Height="100" BorderThickness="2" BorderBrush="Black">
                        <Slider Value="{Binding ., Mode=TwoWay}" Width="300" Minimum="0" Maximum="100" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>